Problem with rails_admin plugin with nested form in has_many: through relationships

I have a problem with a cookie cutter that even has a wiki page associated with it: https://github.com/sferik/rails_admin/wiki/Has-many-%3Athrough-association

I will try to be brief. I have has_many: through relationships in the application that I create. Used models:

Athlete, athlete, SportRole.

The sport_roles table has a list of generic roles that athletes can have, such as the first baseman, the second baseman, etc. The athlete_roles table is a lot for many join tables that have athlete_id and sport_id in them.

My models are defined below with code examples. I just want to create an athlete and associate them with 1+ sports roles (which will eventually create 1+ new entries in the athlete_roles table). He should not ask me for the athlete, since the athlete will not have an identifier until the save to the backend and validation check are called. I don’t need to create new sports here. We assume that all the roles that a new created athlete can create have already been predetermined.

** EDIT **

To clarify, my question is: how do I get one or more existing sports roles for an athlete using the rails_admin plugin, and not in a separate form ? I do not want to create new sports roles, but I want to be able to select one or two when creating an athlete and reflect this data in the athlete_roles table.

The code is 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 
+4
source share
1 answer

I think the problem here is in your model.

The model you are describing has and belongs to many relationships and should look like this:

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 

Migrations should look like this:

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 

attitude of 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 
0
source

All Articles