在 Rails 中通过多态类型查找所有内容?

发布于 2024-08-22 17:51:13 字数 384 浏览 7 评论 0原文

有没有办法在 Rails 中找到特定多态类型的所有多态模型?因此,如果我有组、事件和项目,它们都带有如下声明:

has_many :assignments, :as => :assignable

我可以做这样的事情:

Assignable.all

...或

BuiltInRailsPolymorphicHelper.all("assignable")

那就太好了。

编辑:

...这样 Assignable.all 返回 [Event, Group, Product] (类数组)

Is there a way to find all Polymorphic models of a specific polymorphic type in Rails? So if I have Group, Event, and Project all with a declaration like:

has_many :assignments, :as => :assignable

Can I do something like:

Assignable.all

...or

BuiltInRailsPolymorphicHelper.all("assignable")

That would be nice.

Edit:

... such that Assignable.all returns [Event, Group, Product] (array of classes)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

以歌曲疗慰 2024-08-29 17:51:13

没有直接的方法可以做到这一点。我为 ActiveRecord::Base 编写了这个猴子补丁。
这适用于任何班级。

class ActiveRecord::Base

  def self.all_polymorphic_types(name)
    @poly_hash ||= {}.tap do |hash|
      Dir.glob(File.join(Rails.root, "app", "models", "**", "*.rb")).each do |file|
        klass = File.basename(file, ".rb").camelize.constantize rescue nil
        next unless klass.ancestors.include?(ActiveRecord::Base)

        klass.
          reflect_on_all_associations(:has_many).
          select{ |r| r.options[:as] }.
          each do |reflection|
            (hash[reflection.options[:as]] ||= []) << klass
          end
      end
    end
    @poly_hash[name.to_sym]
  end

end

现在您可以执行以下操作:

Assignable.all_polymorphic_types(:assignable).map(&:to_s)
# returns ['Project', 'Event', 'Group']

There is no direct method for this. I wrote this monkey patch for ActiveRecord::Base.
This will work for any class.

class ActiveRecord::Base

  def self.all_polymorphic_types(name)
    @poly_hash ||= {}.tap do |hash|
      Dir.glob(File.join(Rails.root, "app", "models", "**", "*.rb")).each do |file|
        klass = File.basename(file, ".rb").camelize.constantize rescue nil
        next unless klass.ancestors.include?(ActiveRecord::Base)

        klass.
          reflect_on_all_associations(:has_many).
          select{ |r| r.options[:as] }.
          each do |reflection|
            (hash[reflection.options[:as]] ||= []) << klass
          end
      end
    end
    @poly_hash[name.to_sym]
  end

end

Now you can do the following:

Assignable.all_polymorphic_types(:assignable).map(&:to_s)
# returns ['Project', 'Event', 'Group']
故事与诗 2024-08-29 17:51:13

你也可以尝试这种方式。因为上面的解决方案对我不起作用,因为我有一些 mongo 的模型。

def get_has_many_associations_for_model(associations_name, polymorphic=nil)

  associations_name = associations_name.to_s.parameterize.underscore.pluralize.to_sym
  active_models = ActiveRecord::Base.descendants
  get_model = []
  active_models.each do |model|

    has_many_associations =model.reflect_on_all_associations(:has_many).select{|a|a.name==associations_name }
    has_many_associations = has_many_associations.select{ |a| a.options[:as] == polymorphic.to_s.to_sym} if polymorphic.present?
    get_model << model if has_many_associations.present?

  end
  get_model.map{|a| a.to_s}
end

Anb 如此称呼它,

get_has_many_associations_for_model("assignments", "assignable")

如果您想要多态记录而不是传递它,则第二个参数是可选的,否则将其保留为空白。

它将以字符串形式返回模型名称数组。

You can also try this way.cause above solution doesn't work for me cause i had some mongo's model.

def get_has_many_associations_for_model(associations_name, polymorphic=nil)

  associations_name = associations_name.to_s.parameterize.underscore.pluralize.to_sym
  active_models = ActiveRecord::Base.descendants
  get_model = []
  active_models.each do |model|

    has_many_associations =model.reflect_on_all_associations(:has_many).select{|a|a.name==associations_name }
    has_many_associations = has_many_associations.select{ |a| a.options[:as] == polymorphic.to_s.to_sym} if polymorphic.present?
    get_model << model if has_many_associations.present?

  end
  get_model.map{|a| a.to_s}
end

Anb call it like

get_has_many_associations_for_model("assignments", "assignable")

Here Second parameters is optional for if you want polymorphic records than pass it otherwise leave it as blank.

It will Return Array of Model name as String.

看轻我的陪伴 2024-08-29 17:51:13

Harish Shetty 的解决方案不适用于未直接存储在 Rails.root/app/models 中而是存储在子目录。尽管它正确地全局匹配子目录中的文件,但在将文件名转换为常量时无法包含子目录。原因是,命名空间子目录被这一行删除:

klass = File.basename(file, ".rb").camelize.constantize rescue nil

这是我为保留命名空间子目录所做的操作:

file.sub!(File.join(Rails.root, "app", "models"), '')
file.sub!('.rb', '')
klass = file.classify.constantize rescue nil

这是完整修改的解决方案:

  def self.all_polymorphic_types(name)
    @poly_hash ||= {}.tap do |hash|
      Dir.glob(File.join(Rails.root, "app", "models", "**", "*.rb")).each do |file|
        file.sub!(File.join(Rails.root, "app", "models"), '')
        file.sub!('.rb', '')
        klass = file.classify.constantize rescue nil
        next unless klass.ancestors.include?(ActiveRecord::Base)

        klass.
          reflect_on_all_associations(:has_many).
          select{ |r| r.options[:as] }.
          each do |reflection|
            (hash[reflection.options[:as]] ||= []) << klass
          end
      end
    end
    @poly_hash[name.to_sym]
  end

现在,该方法将 /models/test/tensile.rb 正确地转换为 Test ::在考虑其关联之前拉伸。

只是一个小小的改进,所有的功劳仍然归于 Harish!

Harish Shetty's solution will not work for namespaced model files which are not stored directly in Rails.root/app/models but in a subdirectory. Although it correctly globs files in subdirectories, it then fails to include the subdir when turning the file name into a constant. The reason for this is, that the namespacing subdir is removed by this line:

klass = File.basename(file, ".rb").camelize.constantize rescue nil

Here is what I did to retain the namespacing subdir:

file.sub!(File.join(Rails.root, "app", "models"), '')
file.sub!('.rb', '')
klass = file.classify.constantize rescue nil

Here's the full modified solution:

  def self.all_polymorphic_types(name)
    @poly_hash ||= {}.tap do |hash|
      Dir.glob(File.join(Rails.root, "app", "models", "**", "*.rb")).each do |file|
        file.sub!(File.join(Rails.root, "app", "models"), '')
        file.sub!('.rb', '')
        klass = file.classify.constantize rescue nil
        next unless klass.ancestors.include?(ActiveRecord::Base)

        klass.
          reflect_on_all_associations(:has_many).
          select{ |r| r.options[:as] }.
          each do |reflection|
            (hash[reflection.options[:as]] ||= []) << klass
          end
      end
    end
    @poly_hash[name.to_sym]
  end

Now, the method will turn /models/test/tensile.rb correctly into Test::Tensile before reflecting on its associations.

Just a minor improvement, all credit still goes to Harish!

刘备忘录 2024-08-29 17:51:13

我使用“all”方法创建了一个多态模型类来测试这一点。

class Profile
  # Return all profile instances
  # For class return use 'ret << i' instead of 'ret << i.all'
  def self.all
    ret = []
    subclasses_of(ActiveRecord::Base).each do |i|
      unless i.reflect_on_all_associations.select{|j| j.options[:as] == :profile}.empty?
        ret << i
      end
    end
    ret.flatten
  end

  def self.all_associated
    User.all.map{|u| u.profile }.flatten
  end
end

这是我的应用程序设置:

User < ActiveRecord::Base
  belongs_to :profile, :polymorphic => true
end

Student < ActiveRecord::Base
  has_one :user, :as => :profile
end

I created a polymorphic model class with a method 'all' to test this.

class Profile
  # Return all profile instances
  # For class return use 'ret << i' instead of 'ret << i.all'
  def self.all
    ret = []
    subclasses_of(ActiveRecord::Base).each do |i|
      unless i.reflect_on_all_associations.select{|j| j.options[:as] == :profile}.empty?
        ret << i
      end
    end
    ret.flatten
  end

  def self.all_associated
    User.all.map{|u| u.profile }.flatten
  end
end

Here is my app setup:

User < ActiveRecord::Base
  belongs_to :profile, :polymorphic => true
end

Student < ActiveRecord::Base
  has_one :user, :as => :profile
end
诗酒趁年少 2024-08-29 17:51:13

您应该能够仅使用关联的集合:

model.assignments.all

You should be able to just use the associated collection:

model.assignments.all
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文