Reputation: 2386
So I have two different models one called numbers which is structured like this
t.string :number
t.date :texted
t.integer :times
t.timestamps
and one called Users (I only included the relevant portion)
t.string :phone_number
t.timestamps
What I need to do is grab all numbers that the texted date is more than 2 weeks ago and times is less than 4 and that t.string :number is not equal to any t.string :phone_number in the users table. Do I need to do some sort of sql join to accomplish this?
This is what I have so far:
numbers = Onumber.where("texted <= ? AND times<=4",2.week.ago.utc)
Upvotes: 0
Views: 84
Reputation: 38645
Yes a left join
would be more suitable here:
Onumber.joins(
'left join users on onumbers.number = users.phone_number'
).where(
'users.phone_number is null and onumber.times <= 4 and onumber.texted < ?', 2.weeks.ago.utc
)
This will give you all the numbers not in users.phone_number
, texted
with times
value less than or equal to 4.
Upvotes: 1