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