I have two tables that I'm attempting to do an inner join with.
One is a users
table where the primary key is id
.
Another table is bars
where user_id
is a foreign key. bars
also has a column called foo_id
where food_id
is a foreign key to the foos
table.
I am trying to put together an ActiveRecord query where I can select all users that were created on or before N days ago and do not have any foos
where bars.foo_id
equal to a particular id. I tried doing something like this:
users = User.where("users.created_at <= ?", 50.days.ago).joins(:bars).where("bars.foo_id != 5")
This query fields over 30,000 results, which is incorrect, cause the Users table only has 12,000 rows.
What exactly am I doing wrong here?