# Named\_scope when using foreign keys

**URL:** <https://www.ruby-forum.com/t/named-scope-when-using-foreign-keys/186214>\
**Category:** Rails\
**Created:** [April 2, 2010, 10:07am UTC](https://www.ruby-forum.com/t/named-scope-when-using-foreign-keys/186214 "2010-04-02T10:07:16Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7ab992/32.png) [@jeb](https://www.ruby-forum.com/u/jeb)\
**Post date:** [April 2, 2010, 10:07am UTC](https://www.ruby-forum.com/t/named-scope-when-using-foreign-keys/186214/1 "2010-04-02T10:07:16Z")

</div>

Hi all,

I can’t get this named scope to work. Each store has a number of  
users. Each store can have one manager. I ant to use a nemed scope to  
find the manager by name. Have I got this totally backwards?

Help appreciated  
🙂 jonas

class Store \< ActiveRecord::Base  
has\_many :users, :dependent =\> :destroy  
belongs\_to :manager, :class\_name =\> “User”, :foreign\_key =\>  
“manager\_id”  
named\_scope :manager\_name, lambda{|name| {:include  
=\> :manager, :conditions =\> [‘manager.name = ?’, name]}}

---

<div class="post-metadata">

**Author:** ![jeb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7ab992/32.png) [@jeb](https://www.ruby-forum.com/u/jeb)\
**Post date:** [April 2, 2010, 10:43am UTC](https://www.ruby-forum.com/t/named-scope-when-using-foreign-keys/186214/2 "2010-04-02T10:43:34Z")

</div>

On 2 April 2010 09:06, jeb [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> class Store \< ActiveRecord::Base  
> has\_many :users, :dependent =\> :destroy  
> belongs\_to :manager, :class\_name =\> “User”, :foreign\_key =\>  
> “manager\_id”  
> named\_scope :manager\_name, lambda{|name| {:include  
> =\> :manager, :conditions =\> [‘manager.name = ?’, name]}}

You don’t say what errors you’re getting, but I’d suggest that if your  
tables follow the naming conventions, the conditions should be:  
:conditions =\> [‘managers.name = ?’, name]

---

<div class="post-metadata">

**Author:** ![jeb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7ab992/32.png) [@jeb](https://www.ruby-forum.com/u/jeb)\
**Post date:** [April 2, 2010, 10:58am UTC](https://www.ruby-forum.com/t/named-scope-when-using-foreign-keys/186214/3 "2010-04-02T10:58:12Z")

</div>

On 2 April 2010 09:06, jeb [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> class Store \< ActiveRecord::Base  
> has\_many :users, :dependent =\> :destroy  
> belongs\_to :manager, :class\_name =\> “User”, :foreign\_key =\>  
> “manager\_id”  
> named\_scope :manager\_name, lambda{|name| {:include  
> =\> :manager, :conditions =\> [‘manager.name = ?’, name]}}

That looks ok to me, assuming that the stores table has a manager\_id  
column. Remember that the named scope will return an array of stores  
containing all the stores with a manager of that name.

If you have already a store in @store then to get the managers name  
you just need @store.manager.name of course.

If that doesn’t help post the error you are getting and the code  
around the error.

Colin

---

<div class="post-metadata">

**Author:** ![jeb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7ab992/32.png) [@jeb](https://www.ruby-forum.com/u/jeb)\
**Post date:** [April 3, 2010, 3:03pm UTC](https://www.ruby-forum.com/t/named-scope-when-using-foreign-keys/186214/4 "2010-04-03T15:03:18Z")

</div>

On 3 April 2010 11:42, jeb [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> =\> :users, :conditions =\> [‘users.name = ?’, name]}}
> 
> > > class Store \< ActiveRecord::Base  
> > > has\_many :users, :dependent =\> :destroy  
> > > belongs\_to :manager, :class\_name =\> “User”, :foreign\_key =\>  
> > > “manager\_id”  
> > > named\_scope :manager\_name, lambda{|name| {:include  
> > > =\> :manager, :conditions =\> [‘manager.name = ?’, name]}}

Has it included the managers table? You have not shown enough of the  
sql to see. Try it with :include =\> :managers (plural)

Colin

---

<div class="post-metadata">

**Author:** ![jeb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7ab992/32.png) [@jeb](https://www.ruby-forum.com/u/jeb)\
**Post date:** [April 3, 2010, 12:44pm UTC](https://www.ruby-forum.com/t/named-scope-when-using-foreign-keys/186214/5 "2010-04-03T12:44:00Z")

</div>

Hi,

The error message is:  
SQLite3::SQLException: no such column: manager.name: SELECT  
“stores”.“id” AS t0\_r0, “stores”.“customer\_id” AS t0\_r1, AND ON AND ON

There sure is a name column in the users model.

This works fine but does not give the same result of course:  
named\_scope :manager\_name, lambda{|name| {:include  
=\> :users, :conditions =\> [‘users.name = ?’, name]}}

🙂 j

---

<div class="post-metadata">

**Author:** ![jeb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7ab992/32.png) [@jeb](https://www.ruby-forum.com/u/jeb)\
**Post date:** [April 7, 2010, 2:24pm UTC](https://www.ruby-forum.com/t/named-scope-when-using-foreign-keys/186214/6 "2010-04-07T14:24:05Z")

</div>

There is no managers table, just a relation. Thats the thing:

belongs\_to :manager, :class\_name =\> “User”, :foreign\_key =\>  
“manager\_id”

🙂 j

---

<div class="post-metadata">

**Author:** ![jeb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7ab992/32.png) [@jeb](https://www.ruby-forum.com/u/jeb)\
**Post date:** [April 7, 2010, 10:39pm UTC](https://www.ruby-forum.com/t/named-scope-when-using-foreign-keys/186214/7 "2010-04-07T22:39:23Z")

</div>

On 7 April 2010 13:21, jeb [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> There is no managers table, just a relation. Thats the thing:
> 
> belongs\_to :manager, :class\_name =\> “User”, :foreign\_key =\>  
> “manager\_id”

So has it included the users table then? Post the whole sql.

Colin

---

<div class="post-metadata">

**Author:** ![jeb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7ab992/32.png) [@jeb](https://www.ruby-forum.com/u/jeb)\
**Post date:** [April 13, 2010, 10:04am UTC](https://www.ruby-forum.com/t/named-scope-when-using-foreign-keys/186214/8 "2010-04-13T10:04:44Z")

</div>

> > > > > > class Store \< ActiveRecord::Base  
> > > > > > has\_many :users, :dependent =\> :destroy  
> > > > > > belongs\_to :manager, :class\_name =\> “User”, :foreign\_key =\>  
> > > > > > “manager\_id”  
> > > > > > named\_scope :manager\_name, lambda{|name| {:include  
> > > > > > =\> :manager, :conditions =\> [‘manager.name = ?’, name]}}

Had the same problem, try it with

named\_scope :manager\_name, lambda{|name| {:include  
=\> :manager, :conditions =\> [‘managers.name = ?’, name]}}

with managers.name instead of manager,name the condition. worked for  
me.
