简体   繁体   English

HABTM协会

[英]HABTM association

Im trying to create a HABTM association between my products and tags tables. 我正在尝试在我的产品和标签表之间创建HABTM关联。 I ran the automated migration in my terminal as: 我在终端中以以下方式运行自动迁移:

rails generate migration create_products_tags_join_table

then 然后

rake db:migrate

After I ran those commands I assumed that my migration file would look something like what I read on the http://guides.rubyonrails.org/association_basics.html But instead my migration file looks like this 运行这些命令后,我假定迁移文件的外观类似于在http://guides.rubyonrails.org/association_basics.html上读取的内容,但是迁移文件看起来像这样

class CreateProductsTagsJoinTable < ActiveRecord::Migration
  def up
  end

  def down
  end
end

Do I need to hard code in the migration within these methods and then run the rake db:migrate command again? 我是否需要在这些方法中进行硬编码,然后再次运行rake db:migrate命令?

There are way's you can name your migration to get the migration auto generate definition tags but I just do these types of things manually. 您可以通过多种方式来命名迁移,以使迁移自动生成定义标签,但我只是手动进行这些类型的操作。

In your Case you need 3 tables: 在您的案例中,您需要3个表:

  • products 产品展示
  • tags 标签
  • products_tags (alphabetical order matters here. This is your join table). products_tags(此处的字母顺序很重要。这是您的联接表)。

Products Table Migration 产品表迁移

class CreateProductsTable < ActiveRecord::Migration
  def change
    create_table :products do |t|
      # whatever you want here...
    end
  end
end

Tags Table Migration 标签表迁移

class CreateTagsTable < ActiveRecord::Migration
  def change
    create_table :tags do |t|
      # whatever you want here...
    end
  end
end

Join Table Migration 联接表迁移

class CreateProductsTagsJoinTable < ActiveRecord::Migration
  def change
    create_table :products_tags , :id => false, force: true do |t|
      t.integer :product_id
      t.integer :tag_id
    end
  end
end

Pretty sure if you want some stuff filled out, you'd need to generate a model, not a migration. 可以肯定的是,如果您要填写一些东西,则需要生成一个模型,而不是迁移。

So, something like: 因此,类似:

rails generate model productstags Rails生成模型产品标签

That generates the following migration file: 生成以下迁移文件:

20140930204336_create_productstags.rb 20140930204336_create_productstags.rb

class CreateProductstags < ActiveRecord::Migration def change create_table :productstags do |t| 类CreateProductstags <ActiveRecord :: Migration def change create_table:productstags做| t |

  t.timestamps
end

end end 结束

You'd need to define any fields either manually here or in the model generation. 您需要在此处或在模型生成中手动定义任何字段。 But, if you generate a migration you have to define what you're doing. 但是,如果生成迁移,则必须定义正在执行的操作。 Like, say I wanted to add a join_id field that is an integer to my new ProductsTags table. 例如,说我想向我的新ProductsTags表添加一个整数的join_id字段。

rails g migration add_join_id_to_productstags join_id:integer rails g migration add_join_id_to_productstags join_id:integer

That generates the following. 生成以下内容。

class AddJoinIdToProductstags < ActiveRecord::Migration def change add_column :productstags, :join_id, :integer end end class AddJoinIdToProductstags <ActiveRecord :: Migration def change add_column:productstags,:join_id,:integer end end

Pretty sure you just need to go that route. 可以肯定,您只需要走那条路线即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM