在 Rails 辅助方法中,是否可以将列名指定为辅助方法变量,然后使用它?
我想创建一个辅助方法,可以将 Rails 查找的结果转换为句子,在其中指定结果以及用于生成句子的列。例如:
def items_to_sentence(items, label_column)
items.map { |u| u.(label_column) }.to_sentence
end
我只是不确定如何告诉 Rails 使用我指定的列。
感谢您的关注。
I want to create a helper method that can turn the results of a Rails find into a sentence, where I specify the the results, and the column to use for making the sentence. For example:
def items_to_sentence(items, label_column)
items.map { |u| u.(label_column) }.to_sentence
end
I'm just not sure how to tell Rails to use my specified column.
Thanks for looking.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果
items
包含 ActiveRecord 对象(或具有与您的列名称匹配的访问器方法的任何其他对象),那么您可以使用send
:或者等效:
或者,如果太吵了:
If
items
contains ActiveRecord objects (or any other objects that have accessor methods that match up with your column names), then you could usesend
:Or equivalently:
Or, if that's too noisy: