On Jan 12, 1:44 am, Chris Bartlett <[email protected]> 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 [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---