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 ]}} -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 2 April 2010 09:06, jeb <jonas-sgoA4MbTzF0@public.gmane.org> 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 ] -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 2 April 2010 09:06, jeb <jonas-sgoA4MbTzF0@public.gmane.org> wrote:> 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 ]}}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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 On 2 Apr, 10:56, Colin Law <clan...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> On 2 April 2010 09:06, jeb <jo...-sgoA4MbTzF0@public.gmane.org> wrote: > > > > > > > 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 ]}} > > 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-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 3 April 2010 11:42, jeb <jonas-sgoA4MbTzF0@public.gmane.org> wrote:> 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 > > > On 2 Apr, 10:56, Colin Law <clan...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: >> On 2 April 2010 09:06, jeb <jo...-sgoA4MbTzF0@public.gmane.org> wrote: >> >> >> >> >> >> > 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 ]}}Has it included the managers table? You have not shown enough of the sql to see. Try it with :include => :managers (plural) Colin>> >> 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 > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
There is no managers table, just a relation. Thats the thing: belongs_to :manager, :class_name => "User", :foreign_key => "manager_id" :-) j On 3 Apr, 15:01, Colin Law <clan...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> On 3 April 2010 11:42, jeb <jo...-sgoA4MbTzF0@public.gmane.org> wrote: > > > > > > > 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 > > > On 2 Apr, 10:56, Colin Law <clan...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: > >> On 2 April 2010 09:06, jeb <jo...-sgoA4MbTzF0@public.gmane.org> wrote: > > >> > 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 ]}} > > Has it included the managers table? You have not shown enough of the > sql to see. Try it with :include => :managers (plural) > > Colin > > > > > > >> That looks ok to me, assuming that thestorestable has a manager_id > >> column. Remember that the named scope will return an array ofstores > >> containing all thestoreswith 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 > > > -- > > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 7 April 2010 13:21, jeb <jonas-sgoA4MbTzF0@public.gmane.org> 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> > > :-) j > > On 3 Apr, 15:01, Colin Law <clan...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: >> On 3 April 2010 11:42, jeb <jo...-sgoA4MbTzF0@public.gmane.org> wrote: >> >> >> >> >> >> > 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 >> >> > On 2 Apr, 10:56, Colin Law <clan...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: >> >> On 2 April 2010 09:06, jeb <jo...-sgoA4MbTzF0@public.gmane.org> wrote: >> >> >> > 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 ]}} >> >> Has it included the managers table? You have not shown enough of the >> sql to see. Try it with :include => :managers (plural) >> >> Colin >> >> >> >> >> >> >> That looks ok to me, assuming that thestorestable has a manager_id >> >> column. Remember that the named scope will return an array ofstores >> >> containing all thestoreswith 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 >> >> > -- >> > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. >> > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org. >> > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> > For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en. > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
> > >> >> > 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. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.