나는 사용자가 다른 사용자에 따라 자신의 메시지를 받기를 얻을 수있는 작은 트위터 스타일의 마이크로 블로깅 서비스를 짓고 있어요여러 레일에 액티브를 사용하여 조인
나는이 다음 모델 :
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이 ON 메시지 다음에옵니다 .id = follows.followee_id WHERE (follows.follower_id =) 하지만 난 그게 messages.user_id = follows.followee_id 그렇게 할 수있는 방법이 있나요해야? –
Shalmanese