From: Josh Cheek Date: 2011-09-03T22:53:07+09:00 Subject: Re: help on find over find --bcaec501650f9fbd6c04ac09cb53 Content-Type: text/plain; charset=ISO-8859-1 On Thu, Jan 20, 2011 at 5:05 PM, Mauricio Alcayaga wrote: > class User < ActiveRecord::Base > has_many :requests > end > > finally at view: > <%= yearsCalendar(User.requests) # WORKS FINE %> > > This doesn't make sense, an instance of user should have a method `requests`, but not the user class itself. I assume this code doesn't actually work, or it works for some unrelated reason (maybe there is a method requests defined on ActiveRecord::Base or something) On Thu, Jan 20, 2011 at 5:05 PM, Mauricio Alcayaga wrote: > class Request < ActiveRecord::Base > def unRequested > find(:all, conditions => {:state => "unrequested"}) > end > end > > <%= yearsCalendar(Request.unRequested) #WONT WORK , RETURN ERROR: > "LocalJumpError: no block given" %> > > This also doesn't make sense, for the same reason. unRequested is an instance method, not a class method. It should be `def self.unRequested` Anyway, I think Eric probably had it right. http://api.rubyonrails.org/classes/ActiveRecord/FinderMethods.html#method-i-findit says that all returns an array. There should be a way to return a lazy finder object that you can then add further queries to, but I don't really feel like loading Rails up to figure it out (I'd start by looking here http://guides.rubyonrails.org/active_record_querying.html). But you don't want to be creating in memory arrays and then using Ruby's collection methods, that is not a scalable solution. On Sat, Sep 3, 2011 at 8:07 AM, Mauricio Alcayaga wrote: > Ryan Davis wrote in post #1019657: > > On Sep 1, 2011, at 11:31 , Mauricio Alcayaga wrote: > > > >> @activityPeriods = coleccion.map{ |x| [x.VARIABLEFIELDNAME.year, > >> x.VARIABLEFIELDNAME.month] }.uniq > > > > I'm not sure I entirely understand your question... but I think you want > > to look at Object.send. > > thank you Ryan > > anyway, a friend of mine, cristian (zany) zaninovic, gently answer me, > he spoke as: hey bastard, you should use : > > paramA = 'year' > paramB = 'month' > dateFieldName = 'name_of_da_field' > @activityPeriods = coleccion.map{ |x| > [eval("x.#{dateFieldName}.#{paramA}"), > eval("x.#{dateFieldName}.#{paramB}")] }.uniq > > I didn't try already, but think it will work > > -- > Posted via http://www.ruby-forum.com/. > > This is a bad plan. First, you should be selecting these objects through ActiveRecord's query interface. Second, even if you want to use Ruby's collection methods like #map and #uniq, you should do what Ryan said and use send: [x.send("name_of_da_field", "year"), x.send("name_of_da_field", "month")] This will be faster because you don't have to parse Ruby code, and it will be safer because there's no possibility of accidentally evaluating arbitrary code. --bcaec501650f9fbd6c04ac09cb53--