Rails Rails 中 polymorphic 的使用, 以及获取 unscoped 对象属性

chenyunli · October 25, 2017 · Last by chenyunli replied at October 30, 2017 · 4219 hits

如果有什么错误之处,还请大神们指点。

注意:本文小写的变量都为实例变量,如:employee

使用 polymorphic

场景: EmployeeProducthas_many pictures, 但是又不希望创建两个类似的 pictures 表。也就是我们希望EmployeeProduct 可以同时关联上 Picture, 最后能从 pictures 表中各取所需,也可以在 Picture 的每个实例对象中获得所属对象。最终结果:

  • rails 中
employee.pictures
product.pictures
picture.imageable # 是一个具体的 employee 或者 product 对象
  • 存在表里的是:
id name imageable_id imageable_type
1 sun 1 Product
2 moon 1 Employee
  • 示例代码
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
  default_scope { where(published: true) }
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
  • 注意事项

model 中的 imageable 需要与表中的 imageable_idimageable_type 对应,如果你的是 object_idobject_type,那么 model 中应该是 belongs_to :object, polymorphic: true

覆盖 Picture 中的 imageable

上述代码中,Productscope,当你用 picture.imageable 的时候,会把 scope 也带上,有可能你需要的是所有的 imageable,而不是 scope 下的,这个时候可以通过覆盖掉 imageable 这个方法来解决。

比如:

class Picture < ApplicationRecord
  belongs_to :imageable, polymorphic: true

  def imageable
    @imageable ||= imageable_type.constantize.unscoped.find_by_id imageable_id
  end
end

这里面通过 imageable_type 获取类名,constantize 把字符串的类名转化为对象,再调用 unscoped 方法,最后通过 imageable_id 找到具体的 imageable 对象。定义 @imageable 是为了一次请求中不多次查询imageable,因为 rails 会缓存。

根据反馈做出调整

  1. 一开始文中写的是 scope,默认是不会被关联查出来的,现在改正过来了。
  2. 最好不使用 default_scope
  3. 如果非要使用 default_scope 可以用比文中更好一点的解决方法(文中的方法会导致 includes 无效等问题): belongs_to :imageable, (-> { unscoped }), polymorphic: true

scope 是关联上的 scope?看不出你写的 scope 和关联有什么关系,一开始我以为是 default_scope,后来发现关联是会忽视 default_scope 的。。

你那样改写关联,会导致 includes 无效等杂七杂八的问题,用||=缓存的话,也要有相应失效的机制

Reply to mizuhashi

用 picture.imageable 会带上 product 的 scope,也就是查出 published 的 product,所以这边覆盖掉是想查出 unscoped 的 product,你说的 includes 会有问题,但是我们这边暂时没涉及。有什么更好的方案么,欢迎指教 😁

Reply to chenyunli

说说你的查询语句是怎么写的,Product.published.map{|x| x.picutres.map{|x| x.imageable}} 这样?

Reply to mizuhashi

picture.imageable 这样就查出来了啊

Reply to chenyunli

问题是你的 picture 是怎么来的。。Picture.all.map{|x| x.imageable}?这样也是不会带 published 的啊,还是不清楚要怎么重现

Reply to chenyunli

我也很好奇,scope 只是定义了一个查询方法,并不会作为默认查询条件,你是怎么查才导致查出 published 的 product。

Reply to adamshen

写的是 default_scope , 我改一下,写错了

Reply to mizuhashi

default_scope,不是 scope,文章里面写错了

Reply to mizuhashi

default_scope 的话,用 picture.imageable 不会忽略 default_scope 中的条件

Reply to chenyunli

不是很推荐这么做,就像楼上说的,会导致 includes 无效等杂七杂八的问题。

这个本来的 imageable 方法不是单纯地查找并生成一个关联对象这么简单。而是先通过一个中间对象 ActiveRecord::Associations::BelongsToAssociation 判断下对象是否已经缓存,没有的话再 reset_scope、reload。你通过覆盖这个方法,使原本 ar 设计好的一些东西失效了,得不偿失。而且你本来只是想使 Product 的 default_scope 失效,这样一来,所有 imageable 找出来的 model 全部的 default_scope 都失效了。

Reply to adamshen

如果不用覆盖解决,这块还有好的解决方法不?因为不想带着 default_scope。

Products 可以不要 default_scope 吗,让代码少些弯,更好读些?

Reply to chenyunli

最好就是不要用 default_scope,如果非要用的话,可以试试这样,但也不是很好。

belongs_to :imageable, -> { unscope(:where) }, polymorphic: true
Reply to adamshen

嗯嗯,试一试,谢谢😄 😁

Reply to doosolar

明白,谢谢啦

16 Floor has deleted
17 Floor has deleted
18 Floor has deleted
Reply to adamshen

试了,你说的这个可以实现

最好不要用 default_scope,尽量不要用 default_scope,绝壁不要用 default_scope

除非你的这个模型承载的数据在业务中的职能足够单纯,并且不会被扩展或「花式调用」

Reply to IChou

知道啦

Reply to adamshen

belongs_to :imageable, -> { unscope(:where) }, polymorphic: true 这个我查出来的不对。imageable_id 和 imageable 不对应。

You need to Sign in before reply, if you don't have an account, please Sign up first.