Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails-在模型上自动添加关系_Ruby On Rails_Ruby_Rake_Dbmigrate - Fatal编程技术网

Ruby on rails Rails-在模型上自动添加关系

Ruby on rails Rails-在模型上自动添加关系,ruby-on-rails,ruby,rake,dbmigrate,Ruby On Rails,Ruby,Rake,Dbmigrate,我有两种型号: class RecordProduct < ApplicationRecord end class ShopiTagProduct < ApplicationRecord end 此命令用于创建此迁移: class CreateJoinTableRecordProductsShopyTagProducts < ActiveRecord::Migration[5.2] def change create_join_table :reco

我有两种型号:

class RecordProduct < ApplicationRecord
end

class ShopiTagProduct < ApplicationRecord
end
此命令用于创建此迁移:

class CreateJoinTableRecordProductsShopyTagProducts < ActiveRecord::Migration[5.2]
    def change
        create_join_table :record_products, :shopi_tag_products do |t|
            t.references :record_products, foreign_key: true, index: {name: :productId}
            t.references :shopi_tag_products, foreign_key: true, index: {name: :tagId}
        end
    end
end
我有两个问题

第一:这些柱子重复了什么?正确生成列的正确方法是什么


第二:我应该在模型中手动添加关系(has_和_属于_many)还是自动添加?

我不确定在您的情况下是否可以自动将has_many添加到模型中,在这种情况下,我手动添加

运行rails g迁移CreateJoinTableRecordProductsShopyTagProducts记录\u产品:引用:索引shopi\u标记\u产品:引用:索引 将生成此迁移:

class CreateJoinTableRecordProductsShopyTagProducts
t.reference
在这里是多余的,因为您已经使用了
create\u join\u table

rails g migration createjointablerecordproducts shopytagproducts record\u products shopi\u tag\u products
将完成这项工作。 它将生成这样的文件:

class CreateJoinTableRecordProductsShopyTagProducts
您需要取消对索引的注释并将名称分配给它们,以解决长索引名称的问题

作为替代方案,您可以这样做:
rails g model record\u products\u shopi\u tag\u products record\u products:references shopi\u tag\u products:references
它将产生:

class CreateRecordProductsHopitaGProducts
请注意,此处使用的是
create\u table
,而不是
create\u join\u table
,因此在这种情况下,您必须编写
t.references
。 在这个迁移中,您必须添加
,索引:{name:…}
,以解决长索引的问题

我写过这样的模型:
rails g型号记录产品名称:string

rails g model ShopiTagProduct name:string

record_product.rb

class RecordProduct
shopi_tag_product.rb

class ShopiTagProduct
记录_products_shopi_tag_product.rb(如果使用has_且_属于_many,则不需要)

类记录产品shopitagproduct
种子.rb

RecordProduct.destroy\u all
ShopiTagProduct.destroy_all
r=RecordProduct.create!(名称:“foo”)
s=ShopiTagProduct.create!(名称:“酒吧”)
r、 shopi_tag_产品
class CreateJoinTableRecordProductsShopyTagProducts < ActiveRecord::Migration[5.2]
    def change
        create_join_table :record_products, :shopi_tag_products do |t|
            t.references :record_products, foreign_key: true, index: {name: :productId}
            t.references :shopi_tag_products, foreign_key: true, index: {name: :tagId}
        end
    end
end
class CreateJoinTableRecordProductsShopyTagProducts < ActiveRecord::Migration[5.2]
    def change
        create_join_table :record_products, :shopi_tag_products do |t|
            t.references :record_products, foreign_key: true, index: {name: :productId}
            t.references :shopi_tag_products, foreign_key: true, index: {name: :tagId}
        end
    end
end
record_product_id
shopi_tag_product_id
record_products_id
shopi_tag_products_id
$ rake db:seed
[{"record_product_id"=>4, "shopi_tag_product_id"=>4}]