named_scope when using foreign keys

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 :slight_smile: 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 ]}}

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 ]

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

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 ]}}

:slight_smile: j

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 ]}}

:slight_smile: j

> 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 > :slight_smile: 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 ]}}

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

Colin

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

belongs_to :manager, :class_name => "User", :foreign_key => "manager_id"

:slight_smile: j

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

>> >> > 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.