# ActiveRecord association not seen in rake db:migrate

**URL:** <https://www.ruby-forum.com/t/activerecord-association-not-seen-in-rake-db-migrate/93017>\
**Category:** Rails\
**Created:** [April 26, 2007, 5:46pm UTC](https://www.ruby-forum.com/t/activerecord-association-not-seen-in-rake-db-migrate/93017 "2007-04-26T17:46:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rykkers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e5b9ba/32.png) [@rykkers](https://www.ruby-forum.com/u/rykkers)\
**Post date:** [April 26, 2007, 5:46pm UTC](https://www.ruby-forum.com/t/activerecord-association-not-seen-in-rake-db-migrate/93017/1 "2007-04-26T17:46:00Z")

</div>

I’m having trouble getting a migration to work.

I’ve got an Account model with the following association:

has\_one :last\_verified\_payment,  
:class\_name =\> ‘AuditEntry’,  
:order =\> ‘updated\_at DESC’,  
:conditions =\> “transaction\_type LIKE ‘%verify’ AND content LIKE  
‘%txn\_type=subscr\_payment%’”

I’ve got another model Subscription which belongs\_to :account, and has  
a method

def calculate\_last\_payment\_date  
lvp = account.last\_verified\_payment  
…  
end

When I try to use this method in a migration, it throws a NoMethod  
error for :last\_verified\_payment.

This only happens in the migration though. The migration didn’t touch  
either the accounts nor audit\_entries tables. I’ve even gone so far  
as running rake db:migrate under rdebug, and stopping just before the  
call to account.last\_verified\_payment, displaying which Account  
instance it’s using, verifying that the NoMethod is being raised, and  
in a parallel console session, getting the same Account and  
Subscription, and invoking both the  
Subscription#calculate\_last\_payment\_date, and the  
Account#last\_verified\_payment successfully.

Any ideas what’s going on?

---

<div class="post-metadata">

**Author:** ![rykkers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e5b9ba/32.png) [@rykkers](https://www.ruby-forum.com/u/rykkers)\
**Post date:** [April 27, 2007, 12:57am UTC](https://www.ruby-forum.com/t/activerecord-association-not-seen-in-rake-db-migrate/93017/2 "2007-04-27T00:57:47Z")

</div>

I can not verify this from personal experience, but I have seen posts  
indicating that when a migration is run all you have access to is  
database fields not the full model classes.

I think the idea is that the migrations are operating on the database  
not on the model and should use SQL directly for any data manipulation  
that is required within the migration.

Michael

On Apr 26, 8:45 am, “removed\_email\_address@domain.invalid”

---

<div class="post-metadata">

**Author:** ![rykkers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e5b9ba/32.png) [@rykkers](https://www.ruby-forum.com/u/rykkers)\
**Post date:** [April 27, 2007, 1:03am UTC](https://www.ruby-forum.com/t/activerecord-association-not-seen-in-rake-db-migrate/93017/3 "2007-04-27T01:03:48Z")

</div>

On 26-Apr-07, at 6:56 PM, MichaelLatta wrote:

> > ```
> > :conditions => "transaction_type LIKE '%verify' AND  
> > 
> > ```
> > 
> > end  
> > in a parallel console session, getting the same Account and  
> > Subscription, and invoking both the  
> > Subscription#calculate\_last\_payment\_date, and the  
> > Account#last\_verified\_payment successfully.
> > 
> > Any ideas what’s going on?

define your migration class with associations - then you’ll have  
access to the associations.

Class MyMigration \< ActiveRecord::Migration

class Blog\< ActiveRecord::Base  
has\_many :entries  
end

def self.up  
end

…

cheers,  
Jodi
