0

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)

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.