如何在rails 4中创建关联?

时间:2014-10-11 04:47:22

标签: ruby-on-rails-4

如何使用这些关联生成模型:

Owner 
has_many dogs

Dog
belongs_to :owner, has_many :puppies

Puppy
belongs_to :dog

我试过了:

$ rails generate model Owner name:string
$ rails generate model Dog name:string owner_id:references
$ rails generate model Puppy name:string dog_id:references

然后我这样做了:

class Owner < ActiveRecord::Base
  has_many :dogs
end

class Dog < ActiveRecord::Base
  belongs_to :owner
  has_many :puppies
end

class Puppy < ActiveRecord::Base
  belongs_to :dog
end

但是当我跑步时:

$ bundle exec rake db:migrate

我收到此错误:

==  CreateOwners: migrating ==================================================
-- create_table(:owners)
   -> 0.0063s
==  CreateOwners: migrated (0.0064s) =========================================

==  CreateDogs: migrating ==============================================
-- create_table(:dogs)
rake aborted!
An error has occurred, this and all later migrations canceled:

wrong number of arguments (1 for 0)/Users/7stud/rails_projects/test1/db/migrate/20141011043159_create_dogs.rb:4:in `block in change'

......这里是20141011043159_create_dogs.rb

class CreateDogs < ActiveRecord::Migration
  def change
    create_table :dogs do |t|
      t.name :string
      t.references :owner_id, index: true

      t.timestamps
    end
  end
end

完整的错误消息:

~/rails_projects/test1$ rails generate model Owner name:string
      invoke  active_record
      create    db/migrate/20141011051648_create_owners.rb
      create    app/models/owner.rb
      invoke    test_unit
      create      test/models/owner_test.rb
      create      test/fixtures/owners.yml

~/rails_projects/test1$ rails generate model Dog name:string owner:references
      invoke  active_record
      create    db/migrate/20141011051712_create_dogs.rb
      create    app/models/dog.rb
      invoke    test_unit
      create      test/models/dog_test.rb
      create      test/fixtures/dogs.yml

~/rails_projects/test1$ rails generate model Puppy name:string dog:references
      invoke  active_record
      create    db/migrate/20141011051736_create_puppies.rb
      create    app/models/puppy.rb
      invoke    test_unit
      create      test/models/puppy_test.rb
      create      test/fixtures/puppies.yml

然后我编辑模型看起来像这样:

class Owner < ActiveRecord::Base
  has_many :dogs
end

class Dog < ActiveRecord::Base
  belongs_to :owner
  has_many :puppies
end

class Puppy < ActiveRecord::Base
  belongs_to :dog
end


~/rails_projects/test1$ bundle exec rake db:migrate
==  CreateOwners: migrating ===================================================
-- create_table(:owners)
   -> 0.0063s
==  CreateOwners: migrated (0.0065s) ==========================================

==  CreateDogs: migrating =====================================================
-- create_table(:dogs)
   -> 0.0017s
==  CreateDogs: migrated (0.0018s) ============================================

==  CreatePuppies: migrating ==================================================
-- create_table(:puppies)
   -> 0.0017s
==  CreatePuppies: migrated (0.0019s) =========================================

Yeehaw!它奏效了。

2 个答案:

答案 0 :(得分:0)

您应该从声明中删除_id。 Rails会正确地生成它。 如果你想保持这种方式,请将其声明为整数。

答案 1 :(得分:0)

试试这个:

class CreateDogs < ActiveRecord::Migration
  def change
    create_table :dogs do |t|
      t.string :name
      t.integer :owner_id

      t.timestamps
   add_index :dogs, :owner_id
    end
  end
end