0

関連付けを通じて has many を作成しようとしていますが、すべて間違っているに違いありません。

関連付けコードは正しいと思いますが、結合されたレコードを作成しようとすると、これが得られます。

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) トランザクション SQL の開始 (2.0ms) INSERT INTO "students" ( "created_at", "name", "updated_at") VALUES (?, ?, ?) [["created_at", 2013 年 4 月 18 日 (木) 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 に答える