Hi, I need a fresh set of eyes to look at this bit of controller code and tell me why I might be getting an error message: lookup = Profit.find(:all, :conditions => [ "user_id = ? AND product_id = ?", uid, pid]) unless lookup.empty? if (lookup.updated_on > 360.minutes.ago) return end end If lookup is empty, then I get an error message that lookup.updated_ondoesn''t exist. I''ve tried lookup.nil? but lookup gets returned in the form of [] so that empty works. But if lookup is empty should not the code inside the statement get ignored? Any suggestions on how I might rework that code? Thanks Steve Odom http://www.smarkets.net _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
:all returns an array of objects. :first would retrun the object in question and allow you to do that. I think you want lookup[0] - the first object in the array. On 12/22/05, Steve Odom <steve.odom-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > > I need a fresh set of eyes to look at this bit of controller code and tell > me why I might be getting an error message: > > lookup = Profit.find(:all, :conditions => [ "user_id = ? AND > product_id = ?", uid, pid]) > unless lookup.empty? > if (lookup.updated_on > 360.minutes.ago) > return > end > end > > If lookup is empty, then I get an error message that lookup.updated_on > doesn''t exist. I''ve tried lookup.nil? but lookup gets returned in the form > of [] so that empty works. But if lookup is empty should not the code inside > the statement get ignored? > > Any suggestions on how I might rework that code? > > Thanks > Steve Odom > http://www.smarkets.net > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >-- http://brantinteractive.com rbrant-To7aG29cuAKKf2Id4j40wVaTQe2KTcn/@public.gmane.org 4034 skippack pike v. 267.640.2195 f. 215.689.1454
On 12/22/05, Steve Odom <steve.odom-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > > I need a fresh set of eyes to look at this bit of controller code and tell > me why I might be getting an error message: > > lookup = Profit.find(:all, :conditions => [ "user_id = ? AND > product_id = ?", uid, pid]) > unless lookup.empty? > if (lookup.updated_on > 360.minutes.ago) > return > end > end > > If lookup is empty, then I get an error message that lookup.updated_on > doesn''t exist. I''ve tried lookup.nil? but lookup gets returned in the form > of [] so that empty works. But if lookup is empty should not the code inside > the statement get ignored? > > Any suggestions on how I might rework that code?Profit.find(:all) will return an array. The array will be empty if none of the conditions can be satisified. Later on, you''re calling updated_on on the array object, and I don''t think that''s a valid method for an array object. You probably want to use Profit.find(:first) (or maybe even Profit.find_by_user_id_and_product_id().
Yep. You guys were both right. I had changed my call from a find_by_user_id to a find(:all) and find all expects there might be more than one in the array. Thanks. Steve On 12/22/05, Joe Van Dyk <joevandyk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > On 12/22/05, Steve Odom <steve.odom-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Hi, > > > > I need a fresh set of eyes to look at this bit of controller code and > tell > > me why I might be getting an error message: > > > > lookup = Profit.find(:all, :conditions => [ "user_id = ? AND > > product_id = ?", uid, pid]) > > unless lookup.empty? > > if (lookup.updated_on > 360.minutes.ago) > > return > > end > > end > > > > If lookup is empty, then I get an error message that lookup.updated_on > > doesn''t exist. I''ve tried lookup.nil? but lookup gets returned in the > form > > of [] so that empty works. But if lookup is empty should not the code > inside > > the statement get ignored? > > > > Any suggestions on how I might rework that code? > > Profit.find(:all) will return an array. The array will be empty if > none of the conditions can be satisified. > > Later on, you''re calling updated_on on the array object, and I don''t > think that''s a valid method for an array object. > > You probably want to use Profit.find(:first) (or maybe even > Profit.find_by_user_id_and_product_id(). > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails