0

Okay, at the moment I have the following in my model:

has_many :current_monitorings, :class_name => "Monitoring",
:conditions => proc { [ 'monitorings.created_at > ?', Time.now.midnight ] }

I want to add a condition to this that checks if the outlet is_active attribute is set to false. I tried doing it like this:

has_many :current_monitorings, :class_name => "Monitoring",
:conditions => proc { [ 'monitorings.created_at > ? AND outlet.is_active = ?', Time.now.midnight, 'false' ] }

However, this doesn't work. I'm probably being stupid, but any help is greatly appreciated!

1 Answer 1

2

In your sql, outlet.is_active should be outlets.is_active. Assuming is_active is a boolean field, just pass false and not "false":

Try this:

has_many :monitorings

def current_monitorings
  monitorings.joins(:outlets).where(
    'monitorings.created_at > ? AND outlets.is_active = ?',
    Time.now.midnight,
    false
  )
end
Sign up to request clarification or add additional context in comments.

3 Comments

This leaves me with PG::Error: ERROR: missing FROM-clause entry for table "outlets"
It shouldn't. It only performs 1 query.
How would I call this from a different model. I'm trying to call this on the user model. However, I need to use this in my dashboard_controller.rb

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.