Named_scope and habtm chaining problem

Can anyone help me with this?

I have a habtm relationship. I defined a named_scope on one side of
it. It works fine except when I chain it. For example:

class Category < ActiveRecord::Base
  has_and_belongs_to_many :items
end

class Item < ActiveRecord::Base
  has_and_belongs_to_many :categories

  named_scope :cat, lambda {|cat| {:include
=> :categories, :conditions => ["categories.id = ?", cat]}}
end

Simple. Easy. Normal, right?

But, let’s see I have the following in my join table
“categories_items”:
category_id | item_id

 1                  1
 1                  2
 2                  2
 2                  3

So I try some console stuff (I’m taking out the object return and just
showing the IDs)

Item.cat(1) # => [1,2]
Item.cat(2) # => [2,3]
Item.cat(1).cat(2) # => []

WAIT! Shouldn’t that return [2] ?

Is this a bug? Or am I misunderstanding something?

If I force the “AND” it works:

Item.cat(1) & Item.cat(2) # => [2]

So the chaining of named scopes seems not to be working here.

Help?!?

-Danimal

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs