Accessing models from migrations

Ok, so now Users need to be associated with Organizations. I’ve
created a migration and added a ‘organization_id’ column to the users
table. I want the default organization_id to be the first
Organization. So I have :default => Organization.find(:first). But
it’s complaining about not being able to find the constant
‘Organization’.

Any ideas? I guess the basic question is: “How can I access model
information from a migration?”

On Jan 14, 2006, at 3:25 PM, Joe Van D. wrote:

Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

Joe-

It seems that rails might be trying to look for User or Organization

inside the migrations namespace. Perhaps you can try calling your
models like this instead:

::User
or
::Organization

That should tell rails to look in the top level namespace. I'm not

sure if this is the reel problem or not but it is worth a try.

Cheers-
-Ezra Z.
WebMaster
Yakima Herald-Republic Newspaper
[email protected]
509-577-7732

it’s complaining about not being able to find the constant
‘Organization’.

Does the Organization model exist? It needs too. I just had this
problem whilst trying out migrations tonight.

HTH, Jord

On 1/14/06, Joe Van D. [email protected] wrote:

Ok, so now Users need to be associated with Organizations. I’ve
created a migration and added a ‘organization_id’ column to the users
table. I want the default organization_id to be the first
Organization. So I have :default => Organization.find(:first). But
it’s complaining about not being able to find the constant
‘Organization’.

Any ideas? I guess the basic question is: “How can I access model
information from a migration?”

Oops, I wasn’t thinking clearly.

I figured out that what I want is to loop through each existing user
and assign them a valid gender, birth date, and Organization. Here’s
the Migration:

class AddGenderChapterBirthdateToUsers < ActiveRecord::Migration
def self.up
add_column :users, :birth_date, :datetime, :default => Time.now
add_column :users, :organization_id, :integer
add_column :users, :gender, :string, :default => “male”

User.find(:all).each do |user|
  user.gender = "male"
  user.organization = Organization.find(:first)
  user.birth_date = Time.now
  user.save
end

end

def self.down
remove_column :users, :birth_date
remove_column :users, :organization_id
remove_column :users, :gender
end
end

And I get this when running rake migrate:
rake aborted!
uninitialized constant AddGenderChapterBirthdateToUsers::User

Joe Van D. <joevandyk@…> writes:

  user.save

And I get this when running rake migrate:
rake aborted!
uninitialized constant AddGenderChapterBirthdateToUsers::User

Does your User model exist?

When I remove app/models/user.rb, I get:

shiny:~/rails/vandyk damon$ rake migrate
(in /Users/damon/rails/vandyk)
== AddGenderChapterBirthdateToUsers: migrating ===============
– add_column(:users, :birth_date, :datetime,
{:default=>Sat Jan 14 18:34:45 CST 2006})
→ 0.0139s
– add_column(:users, :organization_id, :integer)
→ 0.0265s
– add_column(:users, :gender, :string, {:default=>“male”})
→ 0.0140s
rake aborted!
uninitialized constant User

So, how to fix?

Manually update the db schema version to 1 (because this was essentially
treated as a failed migration).
in the db:
UPDATE schema_info set version=1;
The other option here is to remove the 3 fields you’ve added manually
with ALTER TABLE statements.

Now, we make sure we have a model User class.

script/generate model User

We can now re-try the migration.
rake migrate VERSION=0
rake migrate

~/rails/vandyk damon$ rake migrate
(in /Users/damon/rails/vandyk)
== AddGenderChapterBirthdateToUsers: migrating =================
– add_column(:users, :birth_date, :datetime,
{:default=>Sat Jan 14 18:41:31 CST 2006})
→ 0.0138s
– add_column(:users, :organization_id, :integer)
→ 0.0264s
– add_column(:users, :gender, :string, {:default=>“male”})
→ 0.0137s
== AddGenderChapterBirthdateToUsers: migrated (0.0631s) ===========

Works.

HTH,
-damon

On 1/14/06, Damon C. [email protected] wrote:

  user.organization = Organization.find(:first)

end

And I get this when running rake migrate:
rake aborted!
uninitialized constant AddGenderChapterBirthdateToUsers::User

Does your User model exist?

Yes, in app/models/user.rb.

– add_column(:users, :gender, :string, {:default=>“male”})
→ 0.0140s
rake aborted!
uninitialized constant User

So, how to fix?

I’m getting a slightly different error, if you notice. Not sure if
that makes a difference.

– add_column(:users, :organization_id, :integer)
→ 0.0264s
– add_column(:users, :gender, :string, {:default=>“male”})
→ 0.0137s
== AddGenderChapterBirthdateToUsers: migrated (0.0631s) ===========

Works.

Hm, I’m not sure what to do.

On 1/14/06, Damon C. [email protected] wrote:

Also, if you have data, you’ll need to make sure you set up your associations…

class User < ActiveRecord::Base
belongs_to :organization
end

class Organization < ActiveRecord::Base
has_many :users
end

Yes, I have associations in my User model. Notice that I’m getting an
error about an undefined constant, nothing regarding associations or
missing methods.

Joe Van D. <joevandyk@…> writes:

has_many :users
end

I mentioned the associations because the code I sent before wouldn’t
quite work
if you had data without specifying the associations. Not because it
solved your
problem.

Notice that I’m getting an error about an undefined constant

Yes, I see your error. Haven’t been able to reproduce it. I can only
get
“unitialized constant User” by messing with my User class definition,
but never “uninitialized constant
AddGenderChapterBirthdateToUsers::User”.

I sent you a message off list.

-damon

Also, if you have data, you’ll need to make sure you set up your
associations…

class User < ActiveRecord::Base
belongs_to :organization
end

class Organization < ActiveRecord::Base
has_many :users
end

I tried this with some data and it worked ok.

-damon

On 1/16/06, Jordan E. [email protected] wrote:

it’s complaining about not being able to find the constant
‘Organization’.

Does the Organization model exist? It needs too. I just had this
problem whilst trying out migrations tonight.

Yes, it does.

Joe Van D. <joevandyk@…> writes:

On 1/14/06, Damon C. <scales@…> wrote:

Joe Van D. <joevandyk …> writes:

And I get this when running rake migrate:
rake aborted!
uninitialized constant AddGenderChapterBirthdateToUsers::User

Did anyone ever find a solution to this problem? I’m experiencing the
exact
same thing:

uninitialized constant AddWikipageIdToAssets

I made sure to add the associations between the models first, but that
didn’t
fix the problem.

Any help would be much appreciated.

–Jonathan

On Feb 10, 2006, at 6:29 AM, Jonathan Leonard wrote:

Did anyone ever find a solution to this problem? I’m experiencing

–Jonathan

Are you trying to use the model in the same migration that you change
its schema in? If so then this might help:

Using a model after changing its table

Sometimes youâ??ll want to add a column in a migration and populate it
immediately after. In that case, youâ??ll need to make a call to
Base#reset_column_information in order to ensure that the model has
the latest column data from after the new column was added. Example:

class AddPeopleSalary < ActiveRecord::Migration
def self.up
add_column :people, :salary, :integer
Person.reset_column_information
Person.find(:all).each do |p|
p.salary = SalaryCalculator.compute§
end
end
end

Cheers-
-Ezra Z.
WebMaster
Yakima Herald-Republic Newspaper
[email protected]
509-577-7732