我有兩種模式,會議和與會者共享habtm關係。我還有一個會議可以屬於的用戶模型(作爲會議組織者)。rails habtm協會實施問題
class Meeting < ActiveRecord::Base
belongs_to :organizer, :class_name => User, :foreign_key => "organizer_id"
has_and_belongs_to_many :attendees, :class_name => User, :association_foreign_key => "attendee_id"
end
class User < ActiveRecord::Base
has_and_belongs_to_many :meetings, :class_name => Meeting, :association_foreign_key => "meeting_id"
end
,然後我有關係表..
create_table "attendees_meetings", :id => false, :force => true do |t|
t.integer "attendee_id"
t.integer "meeting_id"
end
當我創建一個新的會議,然後引用與會者爲meeting.attendees,我得到一個錯誤。與組織者,meeting.organizer同樣的事情會引發錯誤。我沒有正確設置關係嗎?
m = Meeting.create(:subject => "Test", :location => "Neverland", :body => "A test", :organizer_id => 8)
m.organizer
NoMethodError: undefined method `match' for #<Class:0x00000103d8cf08>
和與會者同樣的(雖然我沒有在目前定義的,但不應該拋出一個錯誤)
1.9.2-p318 :014 > m.attendees
(Object doesn't support #inspect)
=>
如果您評論':attendees'關聯,您是否仍然在創建會議時遇到問題?另外,':class_name'應該包含一個字符串('「User」)而不是實際的類('User')。 – PinnyM