我有@user變量,和我有資源表,然後我有的只是user_id說明和RESOURCE_ID如何使用Active Record通過rails中的關聯獲取這些記錄?
@user.resources.each
作品顯然
@user.favorites.first.resource
工作正常,但我希望所有的資源的收藏表。
@user.favorites.resources
不起作用
resource.rb
belongs_to :category
belongs_to :user
has_many :favorites
has_many :resource_tags
has_many :tags, :through => :resource_tags
user.rb
has_many :resources
has_many :favorites
favorite.rb
belongs_to :resource
belongs_to :user
你可以發佈你的模型?特別是在收藏夾和資源之間尋找'has_many'關係和/或通過收藏夾將用戶連接到資源的'has_many:through'。 – rjz 2012-04-13 02:51:51
爲你加上 – Tallboy 2012-04-13 02:56:26
爲了確保我明白,用戶有很多資源,但其中一些資源是他們的最愛,它是通過收藏夾表定義的。如果是這樣的話,當你真正說的是用戶has_many:favorite_resources通過收藏夾。如果你明確地定義了,那麼你只需要調用@ user.favorite_resources ... – 2012-04-13 03:52:28