0
votes

In Rails 3.2 this worked fine:

class Component < ActiveRecord::Base
  has_and_belongs_to_many :brands
  ...
  scope :branded, ->(b) { includes(:brands).where('brands.id in (?)', b.id) }

Other end of the relationship:

class Brand < ActiveRecord::Base
  has_and_belongs_to_many :components

But I'm upgrading to Rails 4 and it breaks with this message:

Unknown column 'components.brand_ids' in 'where clause'

What's the problem here?

UPDATE

I haven't made the switch to strong parameters yet, and I'm using the protected_attributes gem. Could that be the cause of all this?

1
The error is occurring on that exact line? Why are you performing an IN query for a single id? Just do where("brands.id = ?", b.id)Damien Roche
Yeah, not sure why I was doing that. Problem still exists with that line though.t56k
Please post your has_many and belongs_to declaration of your Component and Brand models. Furthermore check that you run all pending migrations.spickermann
can you post the full error stack along with model files ?Siva

1 Answers

1
votes

Not sure what the branded scope is trying to do, but with the assumption it is to find components that are have a brand X, Component.branded(Brand.find(X))

Maybe try this:

scope :branded, ->(b) { joins(:brands).where(Brand.arel_table[:id].eq b.id)}