0

我正在尝试通过关联创建一个有很多但我一定搞错了。

我相信我的关联代码是正确的,但是当我尝试创建连接记录时,这就是我得到的。

1.9.3p194 :001 > d = Day.find(1) 日负载 (3.9ms) SELECT "days".* FROM "days" WHERE "days"."id" = ? LIMIT 1 [["id", 1]] => # 1.9.3p194 :002 > d.students.create!(:id => 1) (0.1ms) 开始事务 SQL (2.0ms) INSERT INTO "students" ( "created_at", "name", "updated_at") 值 (?, ?, ?) [["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) 回滚事务 ActiveRecord::UnknownAttributeError: 未知属性: day_id

楷模

# == 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
4

1 回答 1

0

您在表中的属性day_students应该是:

  • day_id
  • 学生卡
于 2013-04-18T13:20:20.497 回答