Forum: Ruby on Rails named_scope issue undefined method that is a db column name

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
erik (Guest)
on 2009-05-07 21:01
(Received via mailing list)
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 C. (Guest)
on 2009-05-07 21:05
(Received via mailing list)
On May 7, 5:58 pm, erik <removed_email_address@domain.invalid> 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
erik (Guest)
on 2009-05-07 21:23
(Received via mailing list)
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 C. <removed_email_address@domain.invalid>
erik (Guest)
on 2009-05-07 23:17
(Received via mailing list)
Reading is sometimes magic... OrderTrack.one(....).first does the
trick.
Erik
This topic is locked and can not be replied to.