Bruno
Bruno

Reputation: 45

Rails Join with Hash multiple tables

i'm trying to do a query which returns all the name of users who have animals where it's tipo is 'Cachorro'

So i have 3 tables

User
has_many :animals

attribute sample
t.string :name

Animal 
belongs_to :user
belongs_to :animals

attribute samples
user_id
tipo_id

Tipo
has_many :animals

attribute sample
tipo_animal

So far i didn't figured out how sould i do structure this query inside console:

User.joins(:animals, :tipo).where(animals.tipo.tipo_animal: 'Cachorro')

doesn't work, any idea how to proceed?

Upvotes: 2

Views: 884

Answers (1)

user229044
user229044

Reputation: 239382

You can't use the bare words animals.tipo.tipo_animal in Ruby. That makes no sense to Ruby, those aren't things which are defined in the current scope.

You need to give ActiveRecord a string containing that column name:

...where("animals.tipo.tipo_animal" => 'Cachorro')

Upvotes: 1

Related Questions