Ammar
Ammar

Reputation: 1101

Rails 3.2 Query Format Postgres

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!

Upvotes: 0

Views: 262

Answers (1)

Austin
Austin

Reputation: 3890

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

Upvotes: 2

Related Questions