1

私はRailsにかなり慣れていませんが、私が進歩しているのを見るのは良い気分ですが、最初の実際の複雑なモデルの問題に直面しています:/

これが私のモデルがどのように見えるかです:

class Top < ActiveRecord::Base
  has_many :line_tops, :dependent => :destroy
  has_many :ideas, :through => :line_tops
  has_many :top_subscriptions
  has_many :users, :through => :top_subscriptions

class User < ActiveRecord::Base
  has_many :top_suscribtions
  has_many :tops, :through => :top_suscribtions
  has_many :idea_suscribtions
  has_many :ideas, :through => :idea_suscribtions

class IdeaSubscription < ActiveRecord::Base
  belongs_to :idea
  belongs_to :user
  attr_accessible :idea, :user, :done (is a boolean)

トップ、このトップのすべてのアイデア(done = true)を実行したすべてのユーザーを取得したいと思います。どうしたらいいのかわからないので、かなり迷ってしまいました。トップモデルまたはメソッドの新しい属性?アクティブレコードクエリインターフェイスでそれを行うことはできますか、それとも純粋なSQLを使用する必要がありますか?これを達成するための「最良の」方法は何ですか?

助けてくれてありがとう!

4

1 に答える 1

2

次のようになります。

top = Top.find(top_id)
top.users.includes(:idea_subscriptions).where("idea_subscriptions.done = true")
于 2012-07-13T08:25:41.810 に答える