Rails 活动记录关联错误 - nil:NilClass 的未定义方法 'temp'



当我试图访问json.temp@location.recordings.last.temp…时,我得到了nil:NilClass的未定义方法"temp">

我很困惑为什么这会是因为我有一套has_many。。。

它在控制台中工作:

irb(main):003:0> Location.last.recordings.last.temp
Location Load (0.2ms)  SELECT "locations".* FROM "locations" ORDER BY "locations"."id" DESC LIMIT ?  [["LIMIT", 1]]                                 
Recording Load (0.1ms)  SELECT "recordings".* FROM "recordings" WHERE "recordings"."location_id" = ? ORDER BY "recordings"."id" DESC LIMIT ?  [["location_id", 4], ["LIMIT", 1]]                                               
=> 22  

这是我的代码https://github.com/jrwrest/weather-app

如果有人有任何输入,这将是伟大的数字代码在这里

遵循本课程https://gorails.com/episodes/our-first-api

app/models/location.rb

class Location < ApplicationRecord
has_many :recordings
end

app/models/recording.rb

class Recording < ApplicationRecord
belongs_to :location
end

您可能使用了错误的url。在您的数据库中,位置id为4

你在用localhost:3000/api/v1/locations/4.json吗?

相关内容

最新更新