gpt4 book ai didi

ruby-on-rails - rails 4 : how to access an attribute of an ActiveRecord_Relation-Object?

转载 作者:行者123 更新时间:2023-12-04 03:49:16 25 4
gpt4 key购买 nike

我有一个国家模型和一个游记模型。一个国家有很多游记,一篇游记属于一个国家。

在 Rails 控制台中:

 TravelNote.published.country(248)

[#<TravelNote id: 172, country_id: 248, status: 1, advice_against: 0, published_at: "2012-10-04 07:57:00", created_at: "2014-09-23 09:09:20", updated_at: "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属性?

最佳答案

TravelNote.published.country(248)

返回一个记录数组,因此您必须获取数组的第一项:

TravelNote.published.country(248).first.published_at

关于ruby-on-rails - rails 4 : how to access an attribute of an ActiveRecord_Relation-Object?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26483817/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com