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.
2. 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 Biedenharn http://agileconsultingllc.com
[email protected]
>> class AddPaymentTypeData < ActiveRecord::Migration
class PaymentType < ActiveRecord::Base; end
>>
>> 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.
>
> 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
> Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2)
>
> http://www.wishsight.com => Independent, social wishlist management!
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---