ディスカッション、レスポンス、ユーザーモデルがあります。ディスカッションはユーザーのものであり、多くの応答があります。応答はディスカッションとユーザーに属します。ユーザーには多くのディスカッションがあり、多くの応答があります。
1)ユーザーに属する、または2)ユーザーに属する応答があるすべてのディスカッションを取得したいと思います。しかし、構文がわかりません。
これは、1つのクエリでRails 3構文を使用して実行できますか、それともカスタムSQLを作成する必要がありますか?擬似コード:
@discussions = current_user.discussions # and current_user.discussions where a response belongs to current_user
class Discussion < ActiveRecord::Base
belongs_to :author, class_name: 'User', foreign_key: 'user_id'
end
class Response < ActiveRecord::Base
belongs_to :author, class_name: 'User', foreign_key: 'user_id'
belongs_to :discussion
end
class User < ActiveRecord::Base
has_many :discussions
has_many :responses
end