繁体   English   中英

rails_admin插件在has_many:through关系上使用嵌套表单的问题

[英]rails_admin plugin issue with nested form on a has_many :through relationship

我有一个似乎是一个cookie切割器问题,甚至在这里有一个相关的维基页面: https//github.com/sferik/rails_admin/wiki/Has-many-%3A through- association

我会尽量简短。 我正在构建的应用程序中有一个has_many:through关系。 涉及的模型如下:

运动员,AthleteRole,SportRole。

sport_roles表有一个运动员可以拥有的一般角色列表,如一垒手,二垒手等.runior_roles表是包含athlete_id和sport_id的多对多联接表。

我的模型在下面用代码示例定义。 我只是希望能够创建一名运动员,并将他们与1+运动角色联系起来(最终将在athlete_roles表中创建1个以上的新记录)。 它不应该问我一个Athle_id,因为运动员在后端和验证通过后调用保存之前不会有id。 我不需要在这里创建新的sport_roles。 我们将假设所创建的新运动员可以承担的所有角色已经预定义。

**编辑**

为了澄清,我的问题是,如何使用rails_admin插件为运动员选择一个或多个现有运动角色,而不是单独使用 我不希望创建新的运动角色,但我希望能够在创建运动员时选择现有的一个或两个,并将这些数据反映在athlete_roles表中。

代码如下。

class Athlete < ActiveRecord::Base

   has_many :athlete_roles, :dependent => :delete_all, :autosave => true, :include => :sport_role
   has_many :sport_roles, :through => :athlete_roles

  attr_accessible :first_name
  attr_accessible :middle_name
  attr_accessible :last_name
  attr_accessible :suffix_name
  attr_accessible :birthdate


  # FOR RAILS_ADMIN
  # for a multiselect widget:
  attr_accessible :sport_role_ids
  accepts_nested_attributes_for :athlete_roles, :allow_destroy => true
  attr_accessible :athlete_roles_attributes
end

class AthleteRole < ActiveRecord::Base

  attr_accessible :athlete_id
  attr_accessible :sport_role_id

  # Associations
    belongs_to :athlete
    belongs_to :sport_role

    # validations
    validates :athlete_id,:presence=>true,:uniqueness=>{:scope => [:sport_role_id], :message => "is already associated with this Sport Role"}
  validates :sport_role_id,:presence=> true
end

class SportRole < ActiveRecord::Base

  has_many :athlete_roles, :dependent => :delete_all
  has_many :athletes, :through => :athlete_roles

  attr_accessible :name
  attr_accessible :short_name
  attr_accessible :description
  attr_accessible :created_at
  attr_accessible :updated_at

  attr_accessible :athlete_ids
  attr_accessible :athlete_role_ids

  validates :name, :presence => true
  validates :short_name, :presence => true
  validates :description,:length=>{:maximum => 500, :allow_nil => true}

end

我认为这里的问题存在于你的模型中。

像你所描述的模型,是一个拥有并且属于许多关系 ,应该如下所示:

运动员:

class Athlete < ActiveRecord::Base
  has_and_belongs_to_many :sport_roles
end

体育角色

class SportRole < ActiveRecord::Base
  has_and_belongs_to_many :athletes
end

迁移应该如下所示:

运动员

class CreateAthletes < ActiveRecord::Migration
  def change
    create_table :athletes do |t|
      t.timestamps    
    end
  end
end

体育角色

class CreateSportRoles < ActiveRecord::Migration
  def change
    create_table :sport_roles do |t|
      t.timestamps    
    end
  end
end

运动员与运动角色的关系

class SportRolesAthletes < ActiveRecord::Migration
  def change
    create_table :sport_roles_athletes , :id => false do |t|
      t.integer :sport_role_id
      t.integer :athlete_id
    end
  end
end

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM