可以映射到不同模型类型的模型的 Rails 活动记录关联 [英] Rails Active Record Associations for model which may map to different model types

查看:32
本文介绍了可以映射到不同模型类型的模型的 Rails 活动记录关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个具有 UserProduct 类的 RoR 应用程序.一个用户和一个产品一样有很多照片,但每个人也必须有一个profile_picture.

I'm building an RoR application which has a User and Product classes. A user many have many photos as may a product, but each must also have a single profile_picture.

用户:

class User < ActiveRecord::Base
  has_many :pictures
end

产品:

class Product < ActiveRecord::Base
  has_many :pictures
end

我正在努力定义当前的 pictures 模型:

I'm struggling to define the pictures model which is currently:

class Picture < ActiveRecord::Base
  has_one :user
  has_one :product
end

图片的架构如下(为简洁起见省略了时间戳):

The schema for pictures is as follows (timestamps omitted for brevity):

create_table "pictures", force: true do |t|
  t.string   "image_url"
end

最后我有一个迁移,为用户和产品添加个人资料图片的链接

finally I have a migration to add a link for the profile picture to users and products

class AddPicturesToUsersAndWalks < ActiveRecord::Migration
  def change
    add_column :users, :profile_picture, :picture
    add_column :products, :profile_picture, :picture
  end
end

我已阅读http://guides.rubyonrails.org/association_basics.htmlhttp://guides.rubyonrails.org/migrations.html 我不明白这些应该形成关系或应该将外键存储在数据库中的哪个位置.

I've read through http://guides.rubyonrails.org/association_basics.html and http://guides.rubyonrails.org/migrations.html I don't understand how these relationships should be formed or where in the database the foreign keys should be stored.

我无法查看用户或产品表的架构(rake db:migrate 在运行时不会抱怨)因为架构文件中返回了以下错误(我假设这与profile_picture 在两个但我不确定如何进行:

I cannot view the schema for the user or products table (rake db:migrate does not complain when run) because the following error is returned in the schema file (I assume this is related to the profile_picture in both but im unsure how to procede:

# Could not dump table "users" because of following NoMethodError
#   undefined method `[]' for nil:NilClass

请注意我在 rails 4 上使用 ruby​​ 和 sqlite3 数据库

Please note im using ruby on rails 4 and a sqlite3 database

推荐答案

Rails 文档实际上几乎准确地描述了您应该做什么.

Rails documentation actually describes almost precisely what you should do.

多态关联.

class Picture < ActiveRecord::Base
  belongs_to :imageable, polymorphic: true
  # `imageable` is just a name for you to reference and can by anything
  # It is not a class, a table or anything else
  # It affects only corresponding DB column names
end

class User < ActiveRecord::Base
  has_many :pictures, as: :imageable
  # read as: I am an `imageable`, I can have a picture as one
end

class Product < ActiveRecord::Base
  has_many :pictures, as: :imageable
end

在数据库中,这不仅通过 id 进行关联,还通过模型名称来实现:在相应的列 _id<模型>_type.与已知类名且只需要 id 的简单关联相反.

In database this is accomplished by associating not only through id, but also through a model name: in corresponging columns <model>_id and <model>_type. As opposed to simpler associations where class name is known and only an id is needed.

class CreatePictures < ActiveRecord::Migration
  def change
    create_table :pictures do |t|
      t.string  :data
      t.integer :imageable_id
      t.string  :imageable_type
      t.timestamps
    end
  end
end

这篇关于可以映射到不同模型类型的模型的 Rails 活动记录关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆