Pontek
Pontek

Reputation: 149

complex triple join with condition rails

I'm pretty new to rails but it feels good to see that I progress however I'm facing my first real complex model problem :/

Here is how my models look like :

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)

I want to retrieve for a top, all users who have done all ideas (done=true) of this top. I'm quite lost as I don't really know what should I do. A new attribute in top model or a method ? Can I do it with active record query interface or should i use pure sql ? What is the 'best' way to achieve this ?

Thanks for any help !

Upvotes: 1

Views: 453

Answers (1)

bor1s
bor1s

Reputation: 4113

It should be something like this:

top = Top.find(top_id)
top.users.includes(:idea_subscriptions).where("idea_subscriptions.done = true")

Upvotes: 2

Related Questions