rails has_many through。不能让它工作

时间:2013-04-18 13:14:22

标签: ruby-on-rails has-many-through

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

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

  

1.9.3p194:001> d = Day.find(1)Day Load(3.9ms)SELECT“days”。* FROM“days”WHERE“days”。“id”=? LIMIT 1 [[“id”,1]] => #   1.9.3p194:002> d.students.create!(:id => 1)(0.1ms)开始事务SQ​​L(2.0ms)INSERT INTO“students”(“created_at”,   “name”,“updated_at”)VALUES(?,?,?)[[“created_at”,星期四,4月18日   2013 12:49:25 UTC +00:00],[“name”,nil],[“updated_at”,星期四,4月18日   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

1 个答案:

答案 0 :(得分:0)

day_students表中的属性应为:

  • day_id
  • student_id数据