如何迭代 ActiveRecord 属性,包括 attr_accessor 方法 [英] How to iterate ActiveRecord Attributes, including attr_accessor methods

查看:23
本文介绍了如何迭代 ActiveRecord 属性,包括 attr_accessor 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我到处寻找优雅的解决方案.基本问题似乎是映射到数据库列的 ActiveRecord 属性在 ActiveRecord::Base 中的处理方式与 attr_accessor 方法完全不同.

I've looked everywhere for an elegant solution. The essential problem seems to be that ActiveRecord attributes that map to database columns are handled completely differently in ActiveRecord::Base than attr_accessor methods.

我想做类似的事情:

model.attribute_names.each do |name|
  # do stuff
end

以某种方式还包括 attr_accessor 字段,但不包括任何其他实例方法.我知道这不是内置的,但最优雅的方法是什么?

in a way that also includes attr_accessor fields, but not any other instance methods. I know this in not built-in, but what is the most elegant way to do it?

推荐答案

你无法真正解决这个问题.你可以近似一个黑客,但它永远不会很好地工作.

You can't really solve this. You can approximate a hack, but it's not something that will ever work nicely.

model.attribute_names 应该为您提供所有 ActiveRecord 的,但 attr_accessor 字段不是字段.它们只是普通的 ruby​​ 方法,获得它们的唯一方法是使用 model.instance_methods.

model.attribute_names should get you all the ActiveRecord ones, but the attr_accessor fields are not fields. They are just ordinary ruby methods, and the only way to get them is with model.instance_methods.

你可以做 model.attribute_names + model.instance_methods,但是你必须过滤掉所有其他普通的 ruby​​ 方法 initialize, save 等不切实际.

You could do model.attribute_names + model.instance_methods, but then you'd have to filter out all your other normal ruby methods initialize, save, etc which would be impractical.

为了帮助过滤 instance_methods,您可以将它们与 model.instance_variables 匹配(您必须考虑 @ 登录实例变量),但问题是实例变量在第一次被赋值之前根本不存在.

To help filter the instance_methods you could match them up against model.instance_variables (you'd have to account for the @ sign in the instance variables manually), but the problem with this is that instance variables don't actually exist at all until they are first assigned.

在你的environment.rb中,在任何其他东西被加载之前,在ActiveRecord::中定义你自己的self.attr_accessor基础.然后,这可以包装底层 attr_accessor,但也可以将属性名称保存到私有列表中.然后您可以稍后退出此列表.但是我建议不要这样做......像 attr_accessor 这样的猴子补丁核心语言工具肯定会给你带来很多痛苦.

In your environment.rb, before anything else ever gets loaded, define your own self.attr_accessor in ActiveRecord::Base. This could then wrap the underlying attr_accessor but also save the attribute names to a private list. Then you'd be able to pull out of this list later on. However I'd advise against this... monkey-patching core language facilities like attr_accessor is guaranteed to bring you a lot of pain.

ActiveRecord::Base中定义你自己的custom_attr_accessor,它的作用与Idea 2相同,并在你希望能够检索的代码中使用它属性名称.这将是安全的,因为您不会再破坏内置的 attr_accessor 方法,但是您必须更改所有代码以在必要时使用 custom_attr_accessor

Define your own custom_attr_accessor in ActiveRecord::Base, which does the same thing as Idea 2, and use it in your code where you want to be able to retrieve the attribute names. This would be safe as you won't be clobbering the built-in attr_accessor method any more, but you'll have to change all your code to use custom_attr_accessor where neccessary

总而言之,您想做什么需要了解所有 attr_accessor 字段?如果可以,请尝试从不同的角度看待您的问题.

I guess in summary, what are you trying to do that needs to know about all the attr_accessor fields? Try look at your problem from a different angle if you can.

这篇关于如何迭代 ActiveRecord 属性,包括 attr_accessor 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆