Class method finder and undefined method from association


#1

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.


#2

On Jan 12, 1:44 am, Chris B. removed_email_address@domain.invalid 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


#3

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 C. removed_email_address@domain.invalid