[英]rails_admin plugin issue with nested form on a has_many :through relationship
I have what seems to be a cookie cutter problem that even has an associated wiki page here: https://github.com/sferik/rails_admin/wiki/Has-many-%3Athrough-association 我有一个似乎是一个cookie切割器问题,甚至在这里有一个相关的维基页面: https : //github.com/sferik/rails_admin/wiki/Has-many-%3A through- association
I'll try to be brief. 我会尽量简短。 I have a has_many :through relationship in an application I'm building.
我正在构建的应用程序中有一个has_many:through关系。 The models involved are the following:
涉及的模型如下:
Athlete, AthleteRole, SportRole. 运动员,AthleteRole,SportRole。
The sport_roles table has a list of generic roles that an athlete can have such as first baseman, second baseman, etc. The athlete_roles table is the many to many join table that contains an athlete_id and a sport_id. sport_roles表有一个运动员可以拥有的一般角色列表,如一垒手,二垒手等.runior_roles表是包含athlete_id和sport_id的多对多联接表。
My models are defined below with code examples. 我的模型在下面用代码示例定义。 I simply want to be able to create an Athlete and associate them with 1+ sport roles (which will ultimately create 1+ new records in the athlete_roles table).
我只是希望能够创建一名运动员,并将他们与1+运动角色联系起来(最终将在athlete_roles表中创建1个以上的新记录)。 It shouldn't ask me for an athlete_id as the athlete won't have an id until save is called on the backend and validation passes.
它不应该问我一个Athle_id,因为运动员在后端和验证通过后调用保存之前不会有id。 I don't need to be able to create new sport_roles here.
我不需要在这里创建新的sport_roles。 We'll assume all roles that the new athlete being created can take on have already been predefined.
我们将假设所创建的新运动员可以承担的所有角色已经预定义。
** EDIT ** **编辑**
To clarify, my question is, how do I get to pick one or multiple existing sport roles for an athlete using the rails_admin plugin, NOT in a stand alone form ? 为了澄清,我的问题是,如何使用rails_admin插件为运动员选择一个或多个现有运动角色,而不是单独使用 ? I do not wish to create new sport roles, but I want to be able to pick an existing one or two when creating an athlete and have that data reflected in the athlete_roles table.
我不希望创建新的运动角色,但我希望能够在创建运动员时选择现有的一个或两个,并将这些数据反映在athlete_roles表中。
Code below. 代码如下。
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
I think the problem here resides in your model. 我认为这里的问题存在于你的模型中。
A model like you are describing, is a has and belongs to many relation and should look like so: 像你所描述的模型,是一个拥有并且属于许多关系 ,应该如下所示:
athlete: 运动员:
class Athlete < ActiveRecord::Base
has_and_belongs_to_many :sport_roles
end
sports role 体育角色
class SportRole < ActiveRecord::Base
has_and_belongs_to_many :athletes
end
The migrations should look like: 迁移应该如下所示:
athlete 运动员
class CreateAthletes < ActiveRecord::Migration
def change
create_table :athletes do |t|
t.timestamps
end
end
end
sports role 体育角色
class CreateSportRoles < ActiveRecord::Migration
def change
create_table :sport_roles do |t|
t.timestamps
end
end
end
relation between athletes and sports roles 运动员与运动角色的关系
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.