Rails 4:如何访问ActiveRecord_Relation-Object的属性? [英] Rails 4: how to access an attribute of an ActiveRecord_Relation-Object?
本文介绍了Rails 4:如何访问ActiveRecord_Relation-Object的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个国家模型和一个旅行记录模型。
一个国家有很多旅行记录,而旅行记录属于一个国家。
i have a country model and a travel note model. A country has many travel notes and a travel note belongs to one country.
在Rails控制台中:
in Rails console:
TravelNote.published.country(248)
[#< TravelNote ID:172,country_id:248,状态:1,advice_against:0,published_at: 2012-10-04 07:57:00,created_at: 2014-09-23 09 :09:20,updatedat: 2014-09-23 09:09:20>]
TravelNote.published.country(248).published_at
NameError: undefined local variable or method `published_at' for #<Class:0x00000005a84968>
如何获取该对象的Published_at属性?
How can i get the published_at attribute of this object?
推荐答案
TravelNote.published.country(248)
返回记录数组,因此您必须获取数组的第一项:
returns an array of records, so you have to grab the first item of the array:
TravelNote.published.country(248).first.published_at
这篇关于Rails 4:如何访问ActiveRecord_Relation-Object的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文