Rails 3 中是否有更简单的方法来查找记录并返回其属性之一?

发布于 2024-11-07 03:14:33 字数 109 浏览 1 评论 0原文

我发现自己一遍又一遍地编写以下代码:

MyModel.find(my_id).my_field

我想知道是否有更简单的方法来编写此代码?

I found myself writing the following piece of code over and over again:

MyModel.find(my_id).my_field

I wonder if there is a simpler method to write this ?

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

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

发布评论

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

评论(3

凉城凉梦凉人心 2024-11-14 03:14:33

如果您问是否有更简洁的书写方式……不确定标准查找器是否有。你拥有的东西相当小。只是为了好玩,我为你写了这个:)

class ActiveRecord::Base

  def self.method_missing_with_retrieve_just_a_field(method_called, *args, &block)
    if(method_called.to_s=~/get_/)
      self.find(args[0]).send(method_called.to_s.gsub("get_", ""))
    else
      method_missing_without_retrieve_just_a_field(method_called, *args, &block)
    end
  end

  class << self
    alias_method_chain :method_missing, :retrieve_just_a_field
  end
end

如果你把它作为像crazy_finder.rb这样的文件放在你的配置/初始化程序中,那么你可以说:

MyModel.get_my_field(my_id)

它并没有为你节省太多,但只是觉得写起来会很有趣。

If you are asking if there is more concise way of writing that.. not sure there is with the standard finders. What you have is pretty small. Just for fun I wrote this for you though :)

class ActiveRecord::Base

  def self.method_missing_with_retrieve_just_a_field(method_called, *args, &block)
    if(method_called.to_s=~/get_/)
      self.find(args[0]).send(method_called.to_s.gsub("get_", ""))
    else
      method_missing_without_retrieve_just_a_field(method_called, *args, &block)
    end
  end

  class << self
    alias_method_chain :method_missing, :retrieve_just_a_field
  end
end

If you put this in your config/initializers as some file like crazy_finder.rb you can then just say:

MyModel.get_my_field(my_id)

It doesnt save you much, but just thought it would be fun to write.

素食主义者 2024-11-14 03:14:33

除了 Jake 针对每个模型和每个属性的全局解决方案之外,您还可以轻松定义显式的单独访问器:

class MyModel
  def self.my_field(id)
    find(id).my_field
  end
end

或字段数组:

class MyModel
  class << self
    [:my_field, :other_field].each do |field|
      define_method field do |id|
        find(id).send(field)
      end
    end
  end
end

In addition to Jake's global solution for every model and every attribute, you can easily define explicit individual accessors:

class MyModel
  def self.my_field(id)
    find(id).my_field
  end
end

Or an array of fields:

class MyModel
  class << self
    [:my_field, :other_field].each do |field|
      define_method field do |id|
        find(id).send(field)
      end
    end
  end
end
ゃ人海孤独症 2024-11-14 03:14:33

您是一遍又一遍地对同一资源执行此操作还是对许多不同的资源执行此操作?如果您能为我们提供更好的示例来说明您正在尝试做的事情,那将会非常有帮助,如果您多次这样做,这将有助于为我们提供您多次所做的事情的示例。

如果您对一个资源执行此操作只是为了获得不同的值:

如果您在同一操作中一遍又一遍地执行此操作,那么您就做错了。首先将结果存储在一个变量中,如下所示:

@mymodel = MyModel.find(id)

然后你可以使用

@mymodel.my_field

然后再次(无需再次查找)

@mymodel. different_field

或者如果您想对集合执行此操作,您可以执行以下操作:

@mymodels = MyModel.find([my_first_id, my_second_id, my_third_id])

等..你身边更好的例子将有助于帮助你!

Are you doing this for same resource over and over again or to many different resources? It would really help if you'd give us better example of what you're trying to do, if you're doing that many times, it would help to give us example of what you're doing many times.

If you're doing this for one resource only to get different values:

If you're doing this in same action over and over again, you're doing it wrong. First store your result in a variable like this:

@mymodel = MyModel.find(id)

and then you can use

@mymodel.my_field

and then again (without the need to find it again)

@mymodel.different_field

or if you want to do this for a collection you can do:

@mymodels = MyModel.find([my_first_id, my_second_id, my_third_id])

etc.. better example from your side would help to help you!

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