有没有办法检查Rails 3.1中是否存在has_many关联?

时间:2021-03-22 20:10:57

For example there are some models

例如,有一些模型

class Model_1 < ActiveRecord::Base
   has_many :images, :as => :imageable
end

class Model_2 < ActiveRecord::Base
   # doesn't have has_many association
end
...
class Image < ActiveRecord::Base
    belongs_to :imageable, :polymorphic => true
end

How can I check that model has has_many association? Something like this

如何检查该模型是否具有has_many关联?像这样的东西

class ActiveRecord::Base
    def self.has_many_association_exists?(:association)
        ...
    end
end

And it can be used so

它可以这样使用

Model_1.has_many_association_exists?(:images) # true
Model_2.has_many_association_exists?(:images) # false

Thanks in advance

提前致谢

4 个解决方案

#1


15  

What about reflect_on_association?

那个reflect_on_association怎么样?

Model_1.reflect_on_association(:images)

Or reflect_on_all_associations:

或者reflect_on_all_associations:

associations = Model_1.reflect_on_all_associations(:has_many)
associations.any? { |a| a.name == :images }

#2


1  

You could probably use respond_to?

你可以使用respond_to吗?

class ActiveRecord::Base
    def self.has_many_association_exists?(related)
        self.class.associations.respond_to?(related)
    end
end

#3


1  

I found the following to be the simple way to achieve the desired result:

我发现以下是实现所需结果的简单方法:

ModelName.method_defined?(:method_name)

Example:

例:

Model_1.method_defined?(:images) # true
Model_2.method_defined?(:images) # false

Reference: https://*.com/a/18066069/936494

参考:https://*.com/a/18066069/936494

#4


0  

You could just have a method that tries to access a Model_1 object images inside an exception block like (roughly) :

您可以使用一种方法尝试访问异常块内的Model_1对象图像(粗略地):

begin
  model1_obj.images
rescue
  puts 'No association between model_1 and images'
end

Inside rescue, you can just return false if you like.

在救援中,如果你愿意,你可以返回假。

#1


15  

What about reflect_on_association?

那个reflect_on_association怎么样?

Model_1.reflect_on_association(:images)

Or reflect_on_all_associations:

或者reflect_on_all_associations:

associations = Model_1.reflect_on_all_associations(:has_many)
associations.any? { |a| a.name == :images }

#2


1  

You could probably use respond_to?

你可以使用respond_to吗?

class ActiveRecord::Base
    def self.has_many_association_exists?(related)
        self.class.associations.respond_to?(related)
    end
end

#3


1  

I found the following to be the simple way to achieve the desired result:

我发现以下是实现所需结果的简单方法:

ModelName.method_defined?(:method_name)

Example:

例:

Model_1.method_defined?(:images) # true
Model_2.method_defined?(:images) # false

Reference: https://*.com/a/18066069/936494

参考:https://*.com/a/18066069/936494

#4


0  

You could just have a method that tries to access a Model_1 object images inside an exception block like (roughly) :

您可以使用一种方法尝试访问异常块内的Model_1对象图像(粗略地):

begin
  model1_obj.images
rescue
  puts 'No association between model_1 and images'
end

Inside rescue, you can just return false if you like.

在救援中,如果你愿意,你可以返回假。