我想知道我可以在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