Ruby on rails 多态关联及其表的创建
假设我有这个:Ruby on rails 多态关联及其表的创建,ruby-on-rails,ruby,ruby-on-rails-5,Ruby On Rails,Ruby,Ruby On Rails 5,假设我有这个: class Picture < ApplicationRecord belongs_to :imageable, polymorphic: true end class Employee < ApplicationRecord has_many :pictures, as: :imageable end class Product < ApplicationRecord has_many :pictures, as: :imageable end
class Picture < ApplicationRecord
belongs_to :imageable, polymorphic: true
end
class Employee < ApplicationRecord
has_many :pictures, as: :imageable
end
class Product < ApplicationRecord
has_many :pictures, as: :imageable
end
类图片
这是否需要我按照以下方式精确定义表
class CreatePictures < ActiveRecord::Migration[5.0]
def change
create_table :pictures do |t|
t.string :name
t.integer :imageable_id
t.string :imageable_type
t.timestamps
end
add_index :pictures, [:imageable_type, :imageable_id]
end
end
class CreatePictures
或者我可以用不同的列或类型来定义一些不同的内容吗,例如,在某种程度上,我认为更有效?多态性关联会继续发挥作用吗 多态性关联仅与
\u type
和\u id
列对相关。其他一切由你决定
因此,如果愿意,可以添加其他元数据