Forum: Ruby on Rails Access an attribute in the find method

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
007bac1f27905cd4d5d88315e9b0093a?d=identicon&s=25 Guillaume Loader (loader)
on 2009-03-16 23:05
Hello everyone :)

I need to access an attribute directly into a find. Is that possible?

For instance:

Topics.find(:all, :conditions => expiration_date-1.hour + " < NOW( )")

Here, expiration_date would be an attribute of Topics.

Thank you !
B14575f0ca69f10938fdd67e7156e0e1?d=identicon&s=25 Craig Demyanovich (Guest)
on 2009-03-16 23:22
(Received via mailing list)
I think you'd do something like this:

Topics.find(:all, :conditions => ["expiration_date < ?", Time.now +
1.hour])

See
http://www.railsbrain.com/api/rails-2.2.2/doc/inde...
more examples.

Regards,
Craig


--
Craig Demyanovich
Mutually Human Software
http://mutuallyhuman.com
007bac1f27905cd4d5d88315e9b0093a?d=identicon&s=25 Guillaume Loader (loader)
on 2009-03-16 23:49
It helps, thanks!

But what if I have 2 conditions ?


For instance, is this possible?

Topics.find(:all, :conditions => ["expiration_date < ? AND attribute =",
Time.now + 1.hours], yet_another_variable)

Thank you!
B14575f0ca69f10938fdd67e7156e0e1?d=identicon&s=25 Craig Demyanovich (Guest)
on 2009-03-17 00:15
(Received via mailing list)
Topic.find(:all, :conditions => ["expiration_date < ? AND attribute =
?",
Time.now + 1.hour, yet_another_variable])

In other words, everything goes in the conditions array. The first
element
is the SQL fragment. Every other element is a value to be substituted
for a
? in the SQL fragment. Rails will properly quote strings and dates.

Again, see the find docs [
http://www.railsbrain.com/api/rails-2.2.2/doc/inde...]
for examples. Also, the Rails Guides [
http://guides.rubyonrails.org ] are good, and the section on query
conditions [
http://guides.rubyonrails.org/active_record_queryi... ]
has
more details.

Regards,
Craig


--
Craig Demyanovich
Mutually Human Software
http://mutuallyhuman.com
This topic is locked and can not be replied to.