# Class method finder and undefined method from association

**URL:** <https://www.ruby-forum.com/t/class-method-finder-and-undefined-method-from-association/157474>\
**Category:** Rails\
**Created:** [January 12, 2009, 2:45am UTC](https://www.ruby-forum.com/t/class-method-finder-and-undefined-method-from-association/157474 "2009-01-12T02:45:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dacbo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b9e5f3/32.png) [@dacbo](https://www.ruby-forum.com/u/dacbo)\
**Post date:** [January 12, 2009, 2:45am UTC](https://www.ruby-forum.com/t/class-method-finder-and-undefined-method-from-association/157474/1 "2009-01-12T02:45:06Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dacbo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b9e5f3/32.png) [@dacbo](https://www.ruby-forum.com/u/dacbo)\
**Post date:** [January 12, 2009, 8:19am UTC](https://www.ruby-forum.com/t/class-method-finder-and-undefined-method-from-association/157474/2 "2009-01-12T08:19:11Z")

</div>

On Jan 12, 1:44 am, Chris B. [removed\_email\_address@domain.invalid](mailto: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

---

<div class="post-metadata">

**Author:** ![dacbo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b9e5f3/32.png) [@dacbo](https://www.ruby-forum.com/u/dacbo)\
**Post date:** [January 18, 2009, 7:28am UTC](https://www.ruby-forum.com/t/class-method-finder-and-undefined-method-from-association/157474/3 "2009-01-18T07:28:51Z")

</div>

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](mailto:removed_email_address@domain.invalid)
