On Dec 26, 2:25 am, Julien Estebane <julienesteb...@gmail.com> wrote:
> Hello,
>
> I am having what I might think is a basic problem...I can't get the
> relationships of has_many and belongs_to mapped out correctly in the
> database. I would expect, according to my MODEL and MIGRATION, that there
> would be a desktop_id in the states and environments DATABASE table ?
>
> How can I get the desktop_id, state_id and environment_id columns mapped
> out in the tables ?

There need to be statements in your migrations to add those columns -
rails won't look at the models and work it out from the associations
in there.

You may be confusing things with datamapper (that does do this I
think) or with the fact that if you do rails g model foo
belongs_to:bar then rails will add the belongs_to to the model and the
migration.

Fred.
>
> I ran rake db:migrate before going to the rails console and checking the
> tables...
>
> MODEL:
> ------
>
> user@user-DevMachine:~/dev/myapp/app/models$ cat engine.rb
> class Engine < ActiveRecord::Base
>   # attr_accessible :title, :body
>   belongs_to :environment
>   belongs_to :state
> end
> user@user-DevMachine:~/dev/myapp/app/models$ cat environment.rb
> class Environment < ActiveRecord::Base
>   attr_accessible :description
>   has_many :engines
> end
> user@user-DevMachine:~/dev/myapp/app/models$ cat state.rb
> class State < ActiveRecord::Base
>   attr_accessible :description
>   has_many :engines
> end
>
> MIGRATION:
> ----------
>
> user@user-DevMachine:~/dev/myapp/db/migrate$ cat
> 20121225230016_create_engines.rb
> class CreateEngines < ActiveRecord::Migration
>   def change
>     create_table :engines do |t|
>
>       t.timestamps
>     end
>   end
> end
> user@user-DevMachine:~/dev/myapp/db/migrate$ cat
> 20121225230034_create_states.rb
> class CreateStates < ActiveRecord::Migration
>   def change
>     create_table :states do |t|
>       t.string :description
>
>       t.timestamps
>     end
>   end
> end
> user@user-DevMachine:~/dev/myapp/db/migrate$ cat
> 20121225230118_create_environments.rb
> class CreateEnvironments < ActiveRecord::Migration
>   def change
>     create_table :environments do |t|
>       t.string :description
>       t.timestamps
>     end
>   end
> end
> user@user-DevMachine:~/dev/myapp/db/migrate$
>
> DATABASE:
> ---------
>
> user@user-DevMachine:~/dev/myapp/db/migrate$ rails console
> Loading development environment (Rails 3.2.9)
> 1.9.3-p327 :001 > ActiveRecord::Base.connection.execute("SELECT * FROM
> engines")
>    (1.1ms)  SELECT * FROM engines
>  => [{"id"=>1, "created_at"=>"2012-12-26 01:26:38.789476",
> "updated_at"=>"2012-12-26 01:26:38.789476", 0=>1, 1=>"2012-12-26
> 01:26:38.789476", 2=>"2012-12-26 01:26:38.789476"}]
> 1.9.3-p327 :002 > ActiveRecord::Base.connection.execute("SELECT * FROM
> environments")
>    (0.3ms)  SELECT * FROM environments
>  => [{"id"=>1, "description"=>"warm", "created_at"=>"2012-12-26
> 01:27:40.968920", "updated_at"=>"2012-12-26 01:27:40.968920", 0=>1,
> 1=>"warm", 2=>"2012-12-26 01:27:40.968920", 3=>"2012-12-26
> 01:27:40.968920"}, {"id"=>2, "description"=>"cold",
> "created_at"=>"2012-12-26 01:27:41.055711", "updated_at"=>"2012-12-26
> 01:27:41.055711", 0=>2, 1=>"cold", 2=>"2012-12-26 01:27:41.055711",
> 3=>"2012-12-26 01:27:41.055711"}, {"id"=>3, "description"=>"hot",
> "created_at"=>"2012-12-26 01:27:41.129397", "updated_at"=>"2012-12-26
> 01:27:41.129397", 0=>3, 1=>"hot", 2=>"2012-12-26 01:27:41.129397",
> 3=>"2012-12-26 01:27:41.129397"}, {"id"=>4, "description"=>"freezing",
> "created_at"=>"2012-12-26 01:27:41.284936", "updated_at"=>"2012-12-26
> 01:27:41.284936", 0=>4, 1=>"freezing", 2=>"2012-12-26 01:27:41.284936",
> 3=>"2012-12-26 01:27:41.284936"}]
> 1.9.3-p327 :003 > ActiveRecord::Base.connection.execute("SELECT * FROM
> states")
>    (0.4ms)  SELECT * FROM states
>  => [{"id"=>1, "description"=>"running", "created_at"=>"2012-12-26
> 01:27:40.452140", "updated_at"=>"2012-12-26 01:27:40.452140", 0=>1,
> 1=>"running", 2=>"2012-12-26 01:27:40.452140", 3=>"2012-12-26
> 01:27:40.452140"}, {"id"=>2, "description"=>"stopping",
> "created_at"=>"2012-12-26 01:27:40.536188", "updated_at"=>"2012-12-26
> 01:27:40.536188", 0=>2, 1=>"stopping", 2=>"2012-12-26 01:27:40.536188",
> 3=>"2012-12-26 01:27:40.536188"}, {"id"=>3, "description"=>"restarting",
> "created_at"=>"2012-12-26 01:27:40.613507", "updated_at"=>"2012-12-26
> 01:27:40.613507", 0=>3, 1=>"restarting", 2=>"2012-12-26 01:27:40.613507",
> 3=>"2012-12-26 01:27:40.613507"}, {"id"=>4, "description"=>"creating",
> "created_at"=>"2012-12-26 01:27:40.688855", "updated_at"=>"2012-12-26
> 01:27:40.688855", 0=>4, 1=>"creating", 2=>"2012-12-26 01:27:40.688855",
> 3=>"2012-12-26 01:27:40.688855"}, {"id"=>5, "description"=>"destroying",
> "created_at"=>"2012-12-26 01:27:40.768293", "updated_at"=>"2012-12-26
> 01:27:40.768293", 0=>5, 1=>"destroying", 2=>"2012-12-26 01:27:40.768293",
> 3=>"2012-12-26 01:27:40.768293"}]
> 1.9.3-p327 :004 >
>
> Thanks
>
> Julien Estebane

-- 
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 rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to