Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails where clause over two tables

I have the following model in rails application

category => company => store

Store has a belongs_to company and company has a belongs_to category relationship. now i want to use a where method on a store object to retrieve all stores within the same category.

I would like to have something like this

@stores.nearbys(5).where("stores.company.category_id = xxx")

can somebody give me a tip on this

like image 285
Martin Avatar asked Oct 03 '12 10:10

Martin


1 Answers

where supports nested hash.

@stores.nearbys(5).where(:company => { :category_id => @category.id }, joins: company)
like image 113
Simone Carletti Avatar answered Oct 02 '22 04:10

Simone Carletti