Forum: Ruby on Rails accessing models from migrations

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
38a8230ed3d5c685558b4f0aad3fc74b?d=identicon&s=25 Joe Van Dyk (Guest)
on 2006-01-15 00:25
(Received via mailing list)
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?"
8e44c65ac5b896da534ef2440121c953?d=identicon&s=25 Ezra Zygmuntowicz (Guest)
on 2006-01-17 01:27
(Received via mailing list)
On Jan 14, 2006, at 3:25 PM, Joe Van Dyk wrote:

> Rails mailing list
> Rails@lists.rubyonrails.org
> 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 Zygmuntowicz
WebMaster
Yakima Herald-Republic Newspaper
ezra@yakima-herald.com
509-577-7732
188a96403bfb1b3781ab95fc1bc92241?d=identicon&s=25 Jordan Elver (Guest)
on 2006-01-17 02:01
(Received via mailing list)
> > 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
38a8230ed3d5c685558b4f0aad3fc74b?d=identicon&s=25 Joe Van Dyk (Guest)
on 2006-01-21 15:40
(Received via mailing list)
On 1/14/06, Joe Van Dyk <joevandyk@gmail.com> 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
97a7959f0d9e6c90ddae200520e93067?d=identicon&s=25 Damon Clinkscales (Guest)
on 2006-01-21 15:40
(Received via mailing list)
Joe Van Dyk <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
http://damonclinkscales.com/
38a8230ed3d5c685558b4f0aad3fc74b?d=identicon&s=25 Joe Van Dyk (Guest)
on 2006-01-21 15:40
(Received via mailing list)
On 1/14/06, Damon Clinkscales <scales@pobox.com> 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.
97a7959f0d9e6c90ddae200520e93067?d=identicon&s=25 Damon Clinkscales (Guest)
on 2006-01-21 15:40
(Received via mailing list)
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
http://damonclinkscales.com/
38a8230ed3d5c685558b4f0aad3fc74b?d=identicon&s=25 Joe Van Dyk (Guest)
on 2006-01-21 15:40
(Received via mailing list)
On 1/14/06, Damon Clinkscales <scales@pobox.com> 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.
97a7959f0d9e6c90ddae200520e93067?d=identicon&s=25 Damon Clinkscales (Guest)
on 2006-01-21 15:40
(Received via mailing list)
Joe Van Dyk <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
http://damonclinkscales.com/
38a8230ed3d5c685558b4f0aad3fc74b?d=identicon&s=25 Joe Van Dyk (Guest)
on 2006-01-21 15:40
(Received via mailing list)
On 1/16/06, Jordan Elver <jordan.elver@gmail.com> 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.
Fef8f651fefa7b23683a4e57041474d2?d=identicon&s=25 Jonathan Leonard (johanatan)
on 2006-02-11 06:51
(Received via mailing list)
Joe Van Dyk <joevandyk@...> writes:

>
> On 1/14/06, Damon Clinkscales <scales@...> wrote:
> > Joe Van Dyk <joevandyk <at> ...> 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
6076c22b65b36f5d75c30bdcfb2fda85?d=identicon&s=25 Ezra Zygmuntowicz (Guest)
on 2006-02-12 23:54
(Received via mailing list)
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(p)
       end
     end
   end


Cheers-
-Ezra Zygmuntowicz
WebMaster
Yakima Herald-Republic Newspaper
ezra@yakima-herald.com
509-577-7732
This topic is locked and can not be replied to.