# Named\_scope issue undefined method that is a db column name

**URL:** <https://www.ruby-forum.com/t/named-scope-issue-undefined-method-that-is-a-db-column-name/167431>\
**Category:** Rails\
**Created:** [May 7, 2009, 7:01pm UTC](https://www.ruby-forum.com/t/named-scope-issue-undefined-method-that-is-a-db-column-name/167431 "2009-05-07T19:01:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![erik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5ac83/32.png) [@erik](https://www.ruby-forum.com/u/erik)\
**Post date:** [May 7, 2009, 7:01pm UTC](https://www.ruby-forum.com/t/named-scope-issue-undefined-method-that-is-a-db-column-name/167431/1 "2009-05-07T19:01:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![erik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5ac83/32.png) [@erik](https://www.ruby-forum.com/u/erik)\
**Post date:** [May 7, 2009, 7:05pm UTC](https://www.ruby-forum.com/t/named-scope-issue-undefined-method-that-is-a-db-column-name/167431/2 "2009-05-07T19:05:16Z")

</div>

On May 7, 5:58 pm, erik [removed\_email\_address@domain.invalid](mailto: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

---

<div class="post-metadata">

**Author:** ![erik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5ac83/32.png) [@erik](https://www.ruby-forum.com/u/erik)\
**Post date:** [May 7, 2009, 7:23pm UTC](https://www.ruby-forum.com/t/named-scope-issue-undefined-method-that-is-a-db-column-name/167431/3 "2009-05-07T19:23:28Z")

</div>

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](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![erik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5ac83/32.png) [@erik](https://www.ruby-forum.com/u/erik)\
**Post date:** [May 7, 2009, 9:17pm UTC](https://www.ruby-forum.com/t/named-scope-issue-undefined-method-that-is-a-db-column-name/167431/4 "2009-05-07T21:17:56Z")

</div>

Reading is sometimes magic… OrderTrack.one(…).first does the  
trick.  
Erik
