[英]rails has_many through. doesnt get it to work
I'm trying to create a has many through association but I've must have gotten it all wrong. 我正在尝试创建一个具有许多通过关联的关系,但是我一定弄错了所有这些。
I believe i got the association code right but when I try to create joined records this is what I get. 我相信我的关联代码正确,但是当我尝试创建联接记录时,这就是我所得到的。
1.9.3p194 :001 > d = Day.find(1) Day Load (3.9ms) SELECT "days".* FROM "days" WHERE "days"."id" = ? 1.9.3p194:001> d = Day.find(1)日负荷(3.9ms)选择“ 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 INTO "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 LIMIT 1 [[“ id”,1]] =>#1.9.3p194:002> d.students.create!(:id => 1)(0.1ms)开始事务SQL(2.0ms)插入“ students”( “ [created_at”,“ name”,“ updated_at”)VALUES(?,?,?)[[“ created_at”,2013年4月18日,星期四,12:49:25 UTC +00:00],[“ name”,nil] ,[“ updated_at”,星期四,2013年4月18日12:49:25 UTC +00:00]](0.8ms)回滚事务ActiveRecord :: UnknownAttributeError:未知属性: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 the day_students
table should be: 您在day_students
表中的属性应为:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.