我想知道我可以在Rails中使用關聯的程度。考慮到以下情況:rails has_many:通過has_many:通過
class User < ActiveRecord::Base
has_one :provider
has_many :businesses, :through => :provider
end
class Provider < ActiveRecord::Base
has_many :businesses
has_many :bids, :through => :businesses
belongs_to :user
end
class Business < ActiveRecord::Base
has_many :bids
belongs_to :provider
end
class Bid < ActiveRecord::Base
belongs_to :business
end
我能夠設置這些漂亮的快捷鍵像User.businesses
和Provider.bids
但如何做這樣的事情User.bids
?可以聯想一個聯想嗎?可以這麼說嗎?
這是可能的,但需要小心你的嵌套深度,因爲你可能會陷入你的數據庫和Rails應用程序。話雖如此,我寫了一篇博客文章,詳細介紹瞭如何使用nested_has_many_through完成此操作:http://kconrails.com/2010/01/28/nesting-has_many-through-relationships-in-ruby-on-rails/ – 2010-10-23 03:11:19