0
votes

I'm trying to filter my model by the value an associated model. (I'm trying to filter my variants by store, which is an association of product.

class Variant < ActiveRecord::Base
belongs_to :product

def self.by_store_id(store_id)
    where(:product => {:store_id => store_id})
end

### 
class Product < ActiveRecord::Base
belongs_to :store

class Store < ActiveRecord::Base
has_many :products

Every time I try this, I get this error:

ActiveRecord::StatementInvalid (SQLite3::SQLException: no such column: product.store_id: SELECT COUNT(*) FROM "variant_skus" INNER JOIN "products" ON "products"."id" = "variant_skus"."product_id" WHERE "product"."store_id" = ?)

Why am I getting this error? Store_id is definitely a column on my product table.

1

1 Answers

1
votes

The table is defined, but as it is called products, it won't be able to find your column with a product prefix. You can use where('products.store_id = ?', store_id) in your method.