我有一個有很多與我的團體和客人模型的關係。下面是型號:有很多通過問題
class Group < ActiveRecord::Base
has_many :memberships, :dependent => :destroy
has_many :guests, :through => :memberships
def self.find(group)
self.find_by_name(group).guest.collect{ |x| x.name }
end
end
class Guest < ActiveRecord::Base
has_many :memberships, :dependent => :destroy
has_many :groups, :through => :memberships
end
class Membership < ActiveRecord::Base
belongs_to :guest
belongs_to :group
end
我在控制檯檢查和這裏的結果:
ruby-1.9.2-head :001 > Group.first.guests
=> []
ruby-1.9.2-head :002 > Guest.first.groups
(Object doesn't support #inspect)
=>
爲什麼沒有第二個工作?我試圖檢查組的類,但它總是返回一個錯誤:
ruby-1.9.2-head :005 > Guest.first.groups.class
NoMethodError: undefined method `guest' for nil:NilClass
任何人都可以幫助我嗎?我其實只是試圖遵循本指南:http://millarian.com/programming/ruby-on-rails/quick-tip-has_many-through-checkboxes/
哦,廢話!我沒有意識到我意外地壓倒了它。當我重構時,我認爲我的方法是find_group而不是find。大聲笑!謝謝那個地點的人。 – corroded 2011-02-27 16:10:44