On 15 November 2012 16:06, Jim Ruther Nill <[email protected]> wrote: > > > > On Thu, Nov 15, 2012 at 10:19 PM, Colin Law <[email protected]> wrote: >> >> On 15 November 2012 09:11, Jim Ruther Nill <[email protected]> wrote: >> > ... >> > Colin is right but you can also use class methods so you dont have to >> > worry >> > about >> > adding lambdas >> > >> > def self.today_reservations >> > where('reserved_from > :date AND reserved_to <= :date', date: >> > Date.today) >> > end >> >> Does that have any advantage over using a scope? It has the >> disadvantage that one could not say things like >> Reservation.where(some conditions).today_reservations > > > i don't know if it has any advantage but you can definitely do that. You > can > chain class methods as long as the method returns an active record relation > > def self.foo > # build conditions here > where(conds) > end > > def self.order_method > order(order_here) > end > > then you can use these methods like scopes > > Reservation.foo.order_method.where(foo)
Experimentation shows me that you are right, that is a bit of rails magic that I was not aware of. In fact it seems that one can call any class method on an ActiveRecord relation for the class. Thanks for the education. So the question is, is there any significant difference between a scope with a lambda and a class method performing the same operation? Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit https://groups.google.com/groups/opt_out.

