# N00b here, rake error: Expected x.rb to define x

**URL:** <https://www.ruby-forum.com/t/n00b-here-rake-error-expected-x-rb-to-define-x/158202>\
**Category:** Rails\
**Created:** [January 20, 2009, 2:48pm UTC](https://www.ruby-forum.com/t/n00b-here-rake-error-expected-x-rb-to-define-x/158202 "2009-01-20T14:48:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![groovetrain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b4bc9f/32.png) [@groovetrain](https://www.ruby-forum.com/u/groovetrain)\
**Post date:** [January 20, 2009, 2:48pm UTC](https://www.ruby-forum.com/t/n00b-here-rake-error-expected-x-rb-to-define-x/158202/1 "2009-01-20T14:48:34Z")

</div>

I’ve searched through the forum and google, and to no avail. I cannot  
seem to find the solution to my problem. No one has ever defined this  
as a rake problem. Anyway, it’s when my db:migrate tries to process  
this migration:

class AddPaymentTypeData \< ActiveRecord::Migration  
def self.up  
Payment\_type.delete\_all

```
Payment_type.create(:type => 'Check')
Payment_type.create(:type => 'Credit Card')
Payment_type.create(:type => 'Purchase Order')

```

end

def self.down  
Payment\_type.delete\_all  
end  
end

that it gives me the error:  
rake aborted!  
Expected /Users/joel/Sites/depot/app/models/payment\_type.rb to define  
Payment\_type

I have a payment\_type.rb file:

class PaymentType \< ActiveRecord::Base

has\_many :orders

end

This is in the right place, in the path that the rake task is  
complaining that it isn’t. Does anyone have any idea why this is  
happening? I’m sure someone has run into this before. I’m using  
rails 2.2.2, ruby 1.8.

Thanks in advance.

Groove

---

<div class="post-metadata">

**Author:** ![groovetrain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b4bc9f/32.png) [@groovetrain](https://www.ruby-forum.com/u/groovetrain)\
**Post date:** [January 20, 2009, 2:53pm UTC](https://www.ruby-forum.com/t/n00b-here-rake-error-expected-x-rb-to-define-x/158202/2 "2009-01-20T14:53:52Z")

</div>

Hi –

On Tue, 20 Jan 2009, groovetrain wrote:

> Payment\_type.create(:type =\> ‘Check’)  
> rake aborted!  
> end
> 
> This is in the right place, in the path that the rake task is  
> complaining that it isn’t. Does anyone have any idea why this is  
> happening? I’m sure someone has run into this before. I’m using  
> rails 2.2.2, ruby 1.8.

The short answer is: change Payment\_type to PaymentType in your create  
statments (and everywhere else).

Longer answer:

Rails has a “magic” way of resolving references to unknown constants.  
If you refer, say, to PaymentType, without having defined it, Rails  
does the following:

1. convert “PaymentType” to its canonical “underscore” equivalent,  
“payment\_type”
2. add “.rb” to the end
3. search the file load-path for a file called that  
(“payment\_type.rb”)
4. load the file
5. confidentally assume that PaymentType is now defined

You made it as far as 4 (it found app/models/payment\_type.rb), but  
only by coincidence: you used “Payment\_type”, which also turns into  
“payment\_type.rb”. So the file was found – but at no point inside the  
file did you define a constant called “Payment\_type”. What you really  
want is for Rails to be looking for the constant PaymentType, not  
Payment\_type, and that’s why you need to correct your Payment\_type  
references to PaymentType. (And in any case, you need to use the same  
name for the same constant consistently 🙂

David

–  
David A. Black / Ruby Power and Light, LLC  
Ruby/Rails consulting & training: [http://www.rubypal.com](http://www.rubypal.com)  
Coming in 2009: The Well-Grounded Rubyist ([The Well-Grounded Rubyist](http://manning.com/black2))

[http://www.wishsight.com](http://www.wishsight.com) =\> Independent, social wishlist management!

---

<div class="post-metadata">

**Author:** ![groovetrain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b4bc9f/32.png) [@groovetrain](https://www.ruby-forum.com/u/groovetrain)\
**Post date:** [January 20, 2009, 3:04pm UTC](https://www.ruby-forum.com/t/n00b-here-rake-error-expected-x-rb-to-define-x/158202/3 "2009-01-20T15:04:29Z")

</div>

On 20 Jan 2009, at 13:43, groovetrain wrote:

> I’ve searched through the forum and google, and to no avail. I cannot  
> seem to find the solution to my problem. No one has ever defined this  
> as a rake problem. Anyway, it’s when my db:migrate tries to process  
> this migration:
> 
> class AddPaymentTypeData \< ActiveRecord::Migration  
> def self.up  
> Payment\_type.delete\_all

That should be PaymentType

Fred

---

<div class="post-metadata">

**Author:** ![groovetrain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b4bc9f/32.png) [@groovetrain](https://www.ruby-forum.com/u/groovetrain)\
**Post date:** [January 20, 2009, 3:47pm UTC](https://www.ruby-forum.com/t/n00b-here-rake-error-expected-x-rb-to-define-x/158202/4 "2009-01-20T15:47:47Z")

</div>

On Jan 20, 2009, at 8:53 AM, David A. Black wrote:

> Hi –
> 
> On Tue, 20 Jan 2009, groovetrain wrote:
> 
> > I’ve searched through the forum and google, and to no avail. I  
> > cannot  
> > seem to find the solution to my problem. No one has ever defined  
> > this  
> > as a rake problem. Anyway, it’s when my db:migrate tries to process  
> > this migration:

In addition to David’s answer below, let me add two cautions:

1. Whenever you’re using the model inside a migration, it is a good  
idea to define a minimal copy of the model actually within the  
migration. This guards against future changes to the model class  
(including the model going away!).

1.a. If you ever use the model to manipulate data after changing the  
underlying table (e.g., add\_column), you need to call  
Model.reset\_column\_information so ActiveRecord takes a fresh look at  
the database columns.

1. Having a column named “type” in ActiveRecord signifies the actual  
class name when using Single-Table Inheritance (STI). If you get  
errors later because it can’t find a class called “Check”, remember  
this (and pick a different name; it’s easier than struggling with  
ActiveRecord).

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

> > class AddPaymentTypeData \< ActiveRecord::Migration

```
 class PaymentType < ActiveRecord::Base; end

```

---

<div class="post-metadata">

**Author:** ![groovetrain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b4bc9f/32.png) [@groovetrain](https://www.ruby-forum.com/u/groovetrain)\
**Post date:** [January 20, 2009, 6:46pm UTC](https://www.ruby-forum.com/t/n00b-here-rake-error-expected-x-rb-to-define-x/158202/5 "2009-01-20T18:46:40Z")

</div>

On Jan 20, 2009, at 10:54 AM, groovetrain wrote:

> PaymentType.create(:payment\_type =\> ‘Credit Card’)  
> This may be a superfluous question, but does that mean the the actual  
> object defined by the payment\_type.rb file will ALWAYS be called  
> PaymentType?

You probably could have made the change to “class Payment\_type” in the  
model file, too, but the naming conventions expect that the model  
class is in CamelCase and the file is in under\_score.rb (ok,  
camel\_case.rb, but you get the idea).

-Rob

---

<div class="post-metadata">

**Author:** ![groovetrain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b4bc9f/32.png) [@groovetrain](https://www.ruby-forum.com/u/groovetrain)\
**Post date:** [January 20, 2009, 5:10pm UTC](https://www.ruby-forum.com/t/n00b-here-rake-error-expected-x-rb-to-define-x/158202/6 "2009-01-20T17:10:16Z")

</div>

That did it!

@David, changed Payment\_type to PaymentType in my migration file so it  
now looks like:

class AddPaymentTypeData \< ActiveRecord::Migration  
def self.up  
PaymentType.delete\_all

```
PaymentType.create(:payment_type => 'Check')
PaymentType.create(:payment_type => 'Credit Card')
PaymentType.create(:payment_type => 'Purchase Order')

```

end

def self.down  
PaymentType.delete\_all  
end  
end

@Rob also, changed (obviously) the “type” column to “payment\_type”.

This may be a superfluous question, but does that mean the the actual  
object defined by the payment\_type.rb file will ALWAYS be called  
PaymentType?

Thank you for all of your help.

Groove

On Jan 20, 9:46 am, Rob B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
