私は、ユーザーが他のユーザーを追跡し、そのメッセージのフィードを取得することができ、小さなさえずりスタイルのマイクロブログサービスを構築しています複数のレールにはActiveRecordを使用して参加し
I持って次のモデル:
class Follow < ActiveRecord::Base
belongs_to :follower, :class_name => "User"
belongs_to :followee, :class_name => "User"
end
class User < ActiveRecord::Base
has_many :follows, :foreign_key => 'follower_id',
:class_name => 'Follow'
has_many :followers, :through => :follows
has_many :followed, :foreign_key => 'followee_id',
:class_name => 'Follow'
has_many :followees, :through => :followed
has_many :messages
end
class Message < ActiveRecord::Base
belongs_to :user
end
へ現在のユーザーのフィードを取得、私は次のSQLクエリを実行したい:
SELECT * FROM follows JOIN users JOIN messages WHERE follows.follower_id = current_user.id AND follows.followee_id = users.id AND users.id = messages.user_id;
これを行うのが正しいのActiveRecordの方法は何ですか?
has_and_belongs_to_manyアソシエーション:followed_messages、:FOREIGN_KEY => 'follower_id':CLASS_NAME => 'メッセージ'、:association_foreign_key => が生成する 'followed_id' あなたは次のように指定したSQLクエリを実行することができますSQL: SELECT * FROM messages INNER JOINの後に続くmessages.id =が続きます。WHERE(follows.follower_id =) をfollowee_idが、私はそれが messages.user_id = follows.followee_id する必要があることを行うための方法はありますか? –
Shalmanese