私はUser
2経由のテーブルに関連付けられているすべてのarticles
取得する必要がある状況にいるよ:Railsの複数のテーブルから選択する - [table_1、table_2]までに多数の「記事」がありますか?
- article_accessは:公共の記事の、ユーザ:記事に
- article_favoritesを見るためにユーザーに権限を与えますこれら
をお気に入りに追加しているので、ActiveRecordの中で、あなたはこれを持っているかもしれません:
class User < ActiveRecord::Base
has_many :article_access_tokens
has_many :article_favorites
def articles
unless @articles
ids = article_access_tokens.all(:select => "article_id").map(&:article_id) + article_favorites.all(:select => "article_id").map(&:article_id)
@articles = Article.send(:scoped, :conditions => {:id => ids.uniq})
end
@articles
end
end
それは私に基本的に2つの別のテーブルから読み取るarticles
アソシエーションを与えます。しかし、質問はありますか?右これを行う方法は?
これを行うには何とか1 SQL SELECT呼び出しを行うことはできますか?