Ruby on Rails,从另一个实例变量的属性创建实例变量
我认为这是一个简单的问题,但我似乎无法集中精力来解决它。我想从另一个实例变量的属性创建一个实例变量。 例如:
@posts = Post.find(:all, :conditions => ['day > ?', Date.today], :order => 'day')
我想获取另一个实例变量,其中包含 @posts
中的所有日期。有没有一种方法可以快速获取它而无需创建助手/函数? 谢谢!
I think this is an easy question but I can't seem to wrap my head around it to solve it. I want to create an instance variable from a property of another instance variable.
For example:
@posts = Post.find(:all, :conditions => ['day > ?', Date.today], :order => 'day')
I want to get another instance variable that contains all of the dates from the @posts
. Is there a way of quickly getting it without having to create a helper/function?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
简写
这是The result is an array of the value of every post.day 的
which is a shorthand for
The result is an array of the value of each post.day
韦恩拥有的是合适的。我更喜欢收集而不是地图,但它们是等效的。
请注意,最后的 uniq 将为您提供唯一的值。
What Wayne has is a appropriate. I prefer collect instead of map, but they are equivalent.
Notice, uniq at the end will give you the unique values.