加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 获取多态关联的有效模型列表

发布时间:2020-12-17 01:56:40 所属栏目:百科 来源:网络整理
导读:从 rubyonrails.org获取多态关联示例并使用以下模型: class Picture ActiveRecord::Base belongs_to :imageable,polymorphic: trueendclass Employee ActiveRecord::Base has_many :pictures,as: :imageableendclass Product ActiveRecord::Base has_many :
从 rubyonrails.org获取多态关联示例并使用以下模型:

class Picture < ActiveRecord::Base
  belongs_to :imageable,polymorphic: true
end

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

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

我如何找到有效的imageable_types?

例如.这样它就会回归:[:员工,产品]

解决方法

我的问题是你为什么需要这个?我不完全确定,但我想您可能正在寻找:

Picture.select(:imageable_type).distinct.pluck(:imageable_type)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读