Rails 如何查询关联定义

发布于 2024-12-02 06:37:10 字数 271 浏览 3 评论 0原文

我有很多动态代码,它们将复杂的关系保存在字符串中。 例如:

 "product.country.continent.planet.galaxy.name"

我如何检查这些关系是否存在? 我想要一个如下的方法:

  raise "n00b" unless Product.has_associations?("product.country.planet.galaxy")

我怎样才能实现这个?

I have a lot of dynamic code which keeps complex relations in a string.
ex:

 "product.country.continent.planet.galaxy.name"

How can I check if these relations exist?
I want a method like the following:

  raise "n00b" unless Product.has_associations?("product.country.planet.galaxy")

How could I implement this?

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

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

发布评论

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

评论(3

不必了 2024-12-09 06:37:10

试试这个:

def has_associations?(assoc_str)
  klass = self.class
  assoc_str.split(".").all? do |name| 
    (klass = klass.reflect_on_association(name.to_sym).try(:klass)).present?
  end
end

Try this:

def has_associations?(assoc_str)
  klass = self.class
  assoc_str.split(".").all? do |name| 
    (klass = klass.reflect_on_association(name.to_sym).try(:klass)).present?
  end
end
空气里的味道 2024-12-09 06:37:10

如果这些是活动记录关联,则可以执行以下操作:

current_class = Product
has_associations = true
paths = "country.planet.galaxy".split('.')

paths.each |item|
  association = current_class.reflect_on_association( item )
  if association
    current_class = association.klass
  else
    has_associations = false
  end
end

puts has_association

这将告诉您此特定路径是否具有所有关联。

If these are active record associations, here's how you can do it:

current_class = Product
has_associations = true
paths = "country.planet.galaxy".split('.')

paths.each |item|
  association = current_class.reflect_on_association( item )
  if association
    current_class = association.klass
  else
    has_associations = false
  end
end

puts has_association

And this will tell you if this specific path has all the associations.

野侃 2024-12-09 06:37:10

如果您确实将 AR 关联存储在这样的字符串中,则放置在初始化程序中的这段代码应该可以让您执行您想要的操作。我一生都无法弄清楚你为什么要这样做,但我相信你有你的理由。

class ActiveRecord::Base
  def self.has_associations?(relation_string="")
    klass = self
    relation_string.split('.').each { |j|
      # check to see if this is an association for this model
      # and if so, save it so that we can get the class_name of
      # the associated model to repeat this step
      if assoc = klass.reflect_on_association(j.to_sym)
        klass = Kernel.const_get(assoc.class_name)
      # alternatively, check if this is a method on the model (e.g.: "name")
      elsif klass.instance_method_already_implemented?(j)
        true
      else
        raise "Association/Method #{klass.to_s}##{j} does not exist"
      end
    }
    return true
  end
end

这样,您需要省略初始模型名称,因此对于您的示例,它将是:

Product.has_associations?("country.planet.galaxy")

If indeed you are storing the AR associations in a string like that, this code placed in an initializer should let you do what you want. For the life of me I can't quite figure out why you'd want to do this, but I trust you have your reasons.

class ActiveRecord::Base
  def self.has_associations?(relation_string="")
    klass = self
    relation_string.split('.').each { |j|
      # check to see if this is an association for this model
      # and if so, save it so that we can get the class_name of
      # the associated model to repeat this step
      if assoc = klass.reflect_on_association(j.to_sym)
        klass = Kernel.const_get(assoc.class_name)
      # alternatively, check if this is a method on the model (e.g.: "name")
      elsif klass.instance_method_already_implemented?(j)
        true
      else
        raise "Association/Method #{klass.to_s}##{j} does not exist"
      end
    }
    return true
  end
end

With this you'll need to leave off the initial model name, so for your example it would be:

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