Rails find a specific entry in a scoped table
New to rails....still trying to grasp all the "methods"
Table is called Histories.
@compa = History.scoped(:limit => 10,:conditions => {:name => "IBM"}, :order => "closedate ASC")
Now I want to fin开发者_如何学编程d if that @compa subset of the table contains an entry where the closedate = a specific date. I've tried all sorts of methods, but nothing seems to work.
Example:
if (@compa.has_value?(tmp))
Doesn't work.
Should I work in the subset, or go back to the table and do a new scope? How can I work in the subset...I'm missing something I'm sure.
So you would want something like:
if(@compa.include?(tmp))
To see if @compa has the value, tmp
.
However @compa
contains attributes other than closedate. So you would want:
@compa.find{|o| o['closedate'] == tmp}
This will return nil if it doesn't contain that value, otherwise it will return the object that does have closedate
equal to tmp
.
However why not use SQL to handle this:
Since @compa
is an ActiveRecord Relation(because you called scoped), you can query it more by doing something like:
@compa.where(:closedate => tmp).exists?
Links:
Array#include
Enumerable#find
exists?
精彩评论