I''m new to named_scope so bear with me if this is dumb. I have created two named scopes both using lambda to pass in arguments. One passes in a single argument and works great the other passes in two arguments and doesn''t work and gives me this error: undefined method `last_reference'' for #<Class:0x4464cb4> vendor/rails/activerecord/lib/active_record/base.rb:1833:in `method_missing_without_paginate'' /opt/ruby-enterprise-1.8.6-20090201/lib/ruby/gems/1.8/gems/mislav- will_paginate-2.3.8/lib/will_paginate/finder.rb:170:in `method_missing'' vendor/rails/activerecord/lib/active_record/named_scope.rb:171:in `send'' vendor/rails/activerecord/lib/active_record/named_scope.rb:171:in `method_missing'' vendor/rails/activerecord/lib/active_record/base.rb:2003:in `with_scope'' (__DELEGATION__):2:in `__send__'' (__DELEGATION__):2:in `with_scope'' app/controllers/orders_controller.rb:23:in `show'' The two named_scopes: class OrderTrack < ActiveRecord::Base named_scope :one, lambda {|id,account_id| {:conditions => ["id = ? and account_id = ?",id,account_id]}} named_scope :all, lambda {|account_id| {:conditions => ["account_id = ?",account_id]}} end I believe I need to use lambda to pass in arguments. The named_scope :all works great and ends up doing an sql query like: SELECT * FROM `order_tracks` WHERE (account_id = 344) But the single one that I want to end up doing something like: SELECT * FROM `order_tracks` WHERE (id = 3 and account_id = 344) doesn''t want to work. This is soft of a contrived example, but I''m just trying to see if I understand this stuff. Thanks. Erik
Frederick Cheung
2009-May-07 17:04 UTC
Re: named_scope issue undefined method that is a db column name
On May 7, 5:58 pm, erik <e...-9T/GIyPsA//QT0dZR+AlfA@public.gmane.org> wrote:> I''m new to named_scope so bear with me if this is dumb. > > I have created two named scopes both using lambda to pass in > arguments. One passes in a single argument and works great the other > passes in two arguments and doesn''t work and gives me this error: >I''m going to make a wild guess that this is because you''re assuming that OrderTrack.one(...) is a single object rather than an array (possibly containing only one row) Fred> undefined method `last_reference'' for #<Class:0x4464cb4> > > vendor/rails/activerecord/lib/active_record/base.rb:1833:in > `method_missing_without_paginate'' > /opt/ruby-enterprise-1.8.6-20090201/lib/ruby/gems/1.8/gems/mislav- > will_paginate-2.3.8/lib/will_paginate/finder.rb:170:in > `method_missing'' > vendor/rails/activerecord/lib/active_record/named_scope.rb:171:in > `send'' > vendor/rails/activerecord/lib/active_record/named_scope.rb:171:in > `method_missing'' > vendor/rails/activerecord/lib/active_record/base.rb:2003:in > `with_scope'' > (__DELEGATION__):2:in `__send__'' > (__DELEGATION__):2:in `with_scope'' > app/controllers/orders_controller.rb:23:in `show'' > > The two named_scopes: > class OrderTrack < ActiveRecord::Base > named_scope :one, lambda {|id,account_id| {:conditions => ["id = ? > and account_id = ?",id,account_id]}} > named_scope :all, lambda {|account_id| {:conditions => > ["account_id = ?",account_id]}} > end > > I believe I need to use lambda to pass in arguments. The > named_scope :all works great and ends up doing an sql query like: > SELECT * FROM `order_tracks` WHERE (account_id = 344) > > But the single one that I want to end up doing something like: > SELECT * FROM `order_tracks` WHERE (id = 3 and account_id = 344) > doesn''t want to work. > > This is soft of a contrived example, but I''m just trying to see if I > understand this stuff. > Thanks. > Erik
erik
2009-May-07 17:22 UTC
Re: named_scope issue undefined method that is a db column name
Yeah, that''s right. I thought it was dying before sending the sql query, but i was reading my exception incorrectly. I didn''t believe it was an array because I had seen no_method ''blah'' for <Array:...> before, but it through me off as it said Class. So here''s another dumb question... I can put [0] at the end of my OrderTrack.one....[0] line, but is there a way to make named_scope only return a single object of the activerecord type as opposed to an array? Thanks. Erik On May 7, 10:04 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On May 7, 5:58 pm, erik <e...-9T/GIyPsA//QT0dZR+AlfA@public.gmane.org> wrote:> I''m new to named_scope so bear with me if this is dumb. > > > I have created two named scopes both using lambda to pass in > > arguments. One passes in a single argument and works great the other > > passes in two arguments and doesn''t work and gives me this error: > > I''m going to make a wild guess that this is because you''re assuming > that OrderTrack.one(...) is a single object rather than an array > (possibly containing only one row) > > Fred > > > undefined method `last_reference'' for #<Class:0x4464cb4> > > > vendor/rails/activerecord/lib/active_record/base.rb:1833:in > > `method_missing_without_paginate'' > > /opt/ruby-enterprise-1.8.6-20090201/lib/ruby/gems/1.8/gems/mislav- > > will_paginate-2.3.8/lib/will_paginate/finder.rb:170:in > > `method_missing'' > > vendor/rails/activerecord/lib/active_record/named_scope.rb:171:in > > `send'' > > vendor/rails/activerecord/lib/active_record/named_scope.rb:171:in > > `method_missing'' > > vendor/rails/activerecord/lib/active_record/base.rb:2003:in > > `with_scope'' > > (__DELEGATION__):2:in `__send__'' > > (__DELEGATION__):2:in `with_scope'' > > app/controllers/orders_controller.rb:23:in `show'' > > > The two named_scopes: > > class OrderTrack < ActiveRecord::Base > > named_scope :one, lambda {|id,account_id| {:conditions => ["id = ? > > and account_id = ?",id,account_id]}} > > named_scope :all, lambda {|account_id| {:conditions => > > ["account_id = ?",account_id]}} > > end > > > I believe I need to use lambda to pass in arguments. The > > named_scope :all works great and ends up doing an sql query like: > > SELECT * FROM `order_tracks` WHERE (account_id = 344) > > > But the single one that I want to end up doing something like: > > SELECT * FROM `order_tracks` WHERE (id = 3 and account_id = 344) > > doesn''t want to work. > > > This is soft of a contrived example, but I''m just trying to see if I > > understand this stuff. > > Thanks. > > Erik > >
erik
2009-May-07 19:16 UTC
Re: named_scope issue undefined method that is a db column name
Reading is sometimes magic... OrderTrack.one(....).first does the trick. Erik On May 7, 10:22 am, erik <e...-9T/GIyPsA//QT0dZR+AlfA@public.gmane.org> wrote:> Yeah, that''s right. I thought it was dying before sending the sql > query, but i was reading my exception incorrectly. I didn''t believe > it was an array because I had seen no_method ''blah'' for <Array:...> > before, but it through me off as it said Class. > > So here''s another dumb question... I can put [0] at the end of my > OrderTrack.one....[0] line, but is there a way to make named_scope > only return a single object of the activerecord type as opposed to an > array? > Thanks. > Erik > > On May 7, 10:04 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > > On May 7, 5:58 pm, erik <e...-9T/GIyPsA//QT0dZR+AlfA@public.gmane.org> wrote:> I''m new to named_scope so bear with me if this is dumb. > > > > I have created two named scopes both using lambda to pass in > > > arguments. One passes in a single argument and works great the other > > > passes in two arguments and doesn''t work and gives me this error: > > > I''m going to make a wild guess that this is because you''re assuming > > that OrderTrack.one(...) is a single object rather than an array > > (possibly containing only one row) > > > Fred > > > > undefined method `last_reference'' for #<Class:0x4464cb4> > > > > vendor/rails/activerecord/lib/active_record/base.rb:1833:in > > > `method_missing_without_paginate'' > > > /opt/ruby-enterprise-1.8.6-20090201/lib/ruby/gems/1.8/gems/mislav- > > > will_paginate-2.3.8/lib/will_paginate/finder.rb:170:in > > > `method_missing'' > > > vendor/rails/activerecord/lib/active_record/named_scope.rb:171:in > > > `send'' > > > vendor/rails/activerecord/lib/active_record/named_scope.rb:171:in > > > `method_missing'' > > > vendor/rails/activerecord/lib/active_record/base.rb:2003:in > > > `with_scope'' > > > (__DELEGATION__):2:in `__send__'' > > > (__DELEGATION__):2:in `with_scope'' > > > app/controllers/orders_controller.rb:23:in `show'' > > > > The two named_scopes: > > > class OrderTrack < ActiveRecord::Base > > > named_scope :one, lambda {|id,account_id| {:conditions => ["id = ? > > > and account_id = ?",id,account_id]}} > > > named_scope :all, lambda {|account_id| {:conditions => > > > ["account_id = ?",account_id]}} > > > end > > > > I believe I need to use lambda to pass in arguments. The > > > named_scope :all works great and ends up doing an sql query like: > > > SELECT * FROM `order_tracks` WHERE (account_id = 344) > > > > But the single one that I want to end up doing something like: > > > SELECT * FROM `order_tracks` WHERE (id = 3 and account_id = 344) > > > doesn''t want to work. > > > > This is soft of a contrived example, but I''m just trying to see if I > > > understand this stuff. > > > Thanks. > > > Erik > >