Here is my model:
class Ad < ActiveRecord::Base
attr_accessor :current
has_attached_file :image
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
def current
true unless Time.now>self.expires_on
end
end
Then I am trying this:
Ad.where(location: "Dispenser Show Sidebar Left", current: true)
Here is the error:
Unknown column 'ads.current' in 'where clause'
How can I call current in the where clause?
Convert your current method into a scope:
class Ad < ActiveRecord::Base
attr_accessor :current
has_attached_file :image
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
scope :current, -> {
where(['expires_on < ?', Time.now])
}
end
And then chain it to the where conditions
Ad.where(location: "Dispenser Show Sidebar Left").current
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With