rails has_many through. doesnt get it to work -
i'm trying create has many through association i've must have gotten wrong.
i believe got association code right when try create joined records get.
1.9.3p194 :001 > d = day.find(1) day load (3.9ms) select "days".* "days" "days"."id" = ? limit 1 [["id", 1]] => # 1.9.3p194 :002 > d.students.create!(:id => 1) (0.1ms) begin transaction sql (2.0ms) insert "students" ("created_at", "name", "updated_at") values (?, ?, ?) [["created_at", thu, 18 apr 2013 12:49:25 utc +00:00], ["name", nil], ["updated_at", thu, 18 apr 2013 12:49:25 utc +00:00]] (0.8ms) rollback transaction activerecord::unknownattributeerror: unknown attribute: day_id
models
# == schema information # # table name: students # # id :integer not null, primary key # name :string(255) # created_at :datetime not null # updated_at :datetime not null # class student < activerecord::base attr_accessible :name has_many :days, :through => :days_students has_many :days_students, :dependent => :destroy, :class_name => "daystudent" end # == schema information # # table name: days # # id :integer not null, primary key # created_at :datetime not null # updated_at :datetime not null # class day < activerecord::base #attr_accessible has_many :students, :through => :days_students has_many :days_students, :dependent => :destroy, :class_name => "daystudent" end # == schema information # # table name: day_students # # id :integer not null, primary key # students_id :integer # days_id :integer # created_at :datetime not null # updated_at :datetime not null # class daystudent < activerecord::base attr_accessible :student_id, :day_id belongs_to :day belongs_to :student end
your attributes in day_students
table should be:
- day_id
- student_id
Comments
Post a Comment