Rails 2.3.x, named_scope chaining with INNER JOIN complication

Posted by randombits on Stack Overflow See other posts from Stack Overflow or by randombits
Published on 2010-05-30T03:15:03Z Indexed on 2010/05/30 3:22 UTC
Read the original article Hit count: 465

Filed under:
|
|

I have two hypothetical classes, Foo and Bar. Foo contains many Bars. Bar can only belong to one Foo. Ultimately the SQL query I'm trying to make happen looks like the following:

SELECT * from bar INNER JOIN foo ON bar.foo_id = foo.id where bar.in_use = 0 and bar.customer_id = 1 and foo.category = 0

That query does what I need. Now I'm trying to break the problem down in Rails using chained named_scopes. First, the straight forward in_use and customer_id scopes I have set:

  named_scope :available, :conditions => { :in_use => 0 }
  named_scope :not_available, :conditions => { :in_use => 1 }
  named_scope :customer, lambda { |num| { :conditions => { :customer_id => num } } }

Now the part I'm stuck at, is I'm trying to do something like this in my code:

abar = Bar.available.customer(1).category(0)

how and where do I put the category named_scope to make this work?

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about ruby