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

查看:153
本文介绍了如何循环的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 ,但你不得不过滤掉所有其他正常红宝石方法初始化,省,等这将是不切实际的。

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 ,之前的别的永远的被加载时,定义自己的 self.attr_accessor ​​的ActiveRecord :: Base的。这能换标的 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.

定义你自己的 custom_attr_accessor ​​的ActiveRecord :: Base的,它做同样的事情为理念2,和使用它在你的code要能够检索的属性名称。这将是安全的,因为你不会重挫内置 attr_accessor ​​方法更多,但你必须改变你所有的code使用 custom_attr_accessor ​​其中neccessary

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天全站免登陆