Chris Bartlett
2009-Jan-12 01:44 UTC
Class method finder and undefined method from association
I''m trying to create a method on a model (Note) that finds a subset of records, but banging my head against a brick wall. The code so far: class Note belongs_to :person def self.can_be_viewed find(:all, :conditions => ["notes.created_at <= ?", person.cutoff_date]) end ... class Person has_many :notes def cutoff_date ... end ... Ultimately I want to be able to use @person.notes.can_be_viewed, but I''m getting an "undefined local variable or method `person'' for #<Class:0x8ed10b8>" error. This isn''t surprising - the can_be_viewed method is a class method, so has no way of accessing person.cutoff_date in the finder. All I''m really trying to say here is: 1. A person has a cutoff date (this can vary according to who the logged-in user is, but that''s irrelevant, although the cutoff_date method generates some SQL). 2. Notes can be viewed if they are created before their person''s cutoff_date (note.person.cutoff_date). I''d appreciate any thoughts on this. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2009-Jan-12 07:18 UTC
Re: Class method finder and undefined method from association
On Jan 12, 1:44 am, Chris Bartlett <c.bartl...-wUU9E3n5/m4qAMOr+u8IRA@public.gmane.org> wrote:> I''m trying to create a method on a model (Note) that finds a subset of > records, but banging my head against a brick wall. The code so far: >You need to join the people table, eg, find :all, :joins => :person, :conditions => ["notes.created_at <people.cutoff_date"] Fred> class Note > belongs_to :person > > def self.can_be_viewed > find(:all, :conditions => ["notes.created_at <= ?", > person.cutoff_date]) > end > ... > > class Person > has_many :notes > > def cutoff_date > ... > end > ... > > Ultimately I want to be able to use @person.notes.can_be_viewed, but > I''m getting an "undefined local variable or method `person'' for > #<Class:0x8ed10b8>" error. This isn''t surprising - the can_be_viewed > method is a class method, so has no way of accessing > person.cutoff_date in the finder. > > All I''m really trying to say here is: > 1. A person has a cutoff date (this can vary according to who the > logged-in user is, but that''s irrelevant, although the cutoff_date > method generates some SQL). > 2. Notes can be viewed if they are created before their person''s > cutoff_date (note.person.cutoff_date). > > I''d appreciate any thoughts on this.--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Chris Bartlett
2009-Jan-18 06:27 UTC
Re: Class method finder and undefined method from association
Thanks Fred. That hasn''t done it (I had tried joins), but I''ll keep working on it. On Jan 12, 8:18 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Jan 12, 1:44 am, Chris Bartlett <c.bartl...-wUU9E3n5/m4qAMOr+u8IRA@public.gmane.org> wrote: > > > I''m trying to create a method on a model (Note) that finds a subset of > > records, but banging my head against a brick wall. The code so far: > > You need to join the people table, eg, > > find :all, :joins => :person, :conditions => ["notes.created_at <> people.cutoff_date"] > > Fred > > > class Note > > belongs_to :person > > > def self.can_be_viewed > > find(:all, :conditions => ["notes.created_at <= ?", > > person.cutoff_date]) > > end > > ... > > > class Person > > has_many :notes > > > def cutoff_date > > ... > > end > > ... > > > Ultimately I want to be able to use @person.notes.can_be_viewed, but > > I''m getting an "undefined local variable or method `person'' for > > #<Class:0x8ed10b8>" error. This isn''t surprising - the can_be_viewed > > method is a class method, so has no way of accessing > > person.cutoff_date in the finder. > > > All I''m really trying to say here is: > > 1. A person has a cutoff date (this can vary according to who the > > logged-in user is, but that''s irrelevant, although the cutoff_date > > method generates some SQL). > > 2. Notes can be viewed if they are created before their person''s > > cutoff_date (note.person.cutoff_date). > > > I''d appreciate any thoughts on this.--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---