我正在使用jbuilder来构建json响应。见下文,我的jbuilder模板:
json.array! @items do |item|
json.title item.title
json.description item.description
json.category item.categories.first.title
json.price item.price / 100.0
json.status item.status
if !item.banned?
json.published_date item.published_date
json.seller_name item.seller.name
end
json.seller_latitude item.seller.latitude.to_f
json.seller_longtitude item.seller.longtitude.to_f
end
我可以像这样访问item.categories集合中的对象,没有问题:
item.categories.first
当我尝试从上面从返回的类别对象中提取title属性时,我在浏览器中收到此错误:
undefined method 'title' for nil:NilClass
我在我的jbuilder模板中在此行之前添加了一个byebug语句:
item.categories.first.title
在byebug控制台中我可以访问title属性没问题。请参阅下面的byebug控制台打印:
Started GET "/items.json" for ::1 at 2016-07-16 13:59:45 -0400
Processing by ItemsController#index as JSON
Item Load (0.5ms) SELECT "items".* FROM "items"
[1, 10] in /Users/Macbook/projects/VSC/app/views/items/index.json.jbuilder
1:
2: json.array! @items do |item|
3: json.title item.title
4: json.description item.description
5: byebug
=> 6: json.category item.categories.first.title
7: json.price item.price / 100.0
8: json.status item.status
9:
10: if !item.banned?
(byebug) item.categories.first.title
Category Load (0.3ms) SELECT "categories".* FROM "categories" INNER JOIN "categorizations" ON "categories"."id" = "categorizations"."category_id" WHERE "categorizations"."item_id" = $1 ORDER BY "categories"."id" ASC LIMIT 1 [["item_id", 1]]
"movies"
(byebug)
我很难过,我做错了什么?
编辑:
这是jbuilder呈现视图时触发的sql:
Started GET "/items.json" for ::1 at 2016-07-16 14:24:29 -0400
Processing by ItemsController#index as JSON
Item Load (0.8ms) SELECT "items".* FROM "items"
Category Load (0.5ms) SELECT "categories".* FROM "categories" INNER JOIN "categorizations" ON "categories"."id" = "categorizations"."category_id" WHERE "categorizations"."item_id" = $1 ORDER BY "categories"."id" ASC LIMIT 1 [["item_id", 1]]
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
Category Load (0.5ms) SELECT "categories".* FROM "categories" INNER JOIN "categorizations" ON "categories"."id" = "categorizations"."category_id" WHERE "categorizations"."item_id" = $1 ORDER BY "categories"."id" ASC LIMIT 1 [["item_id", 2]]
CACHE (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
Category Load (0.4ms) SELECT "categories".* FROM "categories" INNER JOIN "categorizations" ON "categories"."id" = "categorizations"."category_id" WHERE "categorizations"."item_id" = $1 ORDER BY "categories"."id" ASC LIMIT 1 [["item_id", 3]]
CACHE (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
Category Load (0.5ms) SELECT "categories".* FROM "categories" INNER JOIN "categorizations" ON "categories"."id" = "categorizations"."category_id" WHERE "categorizations"."item_id" = $1 ORDER BY "categories"."id" ASC LIMIT 1 [["item_id", 4]]
CACHE (3.8ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
Category Load (0.5ms) SELECT "categories".* FROM "categories" INNER JOIN "categorizations" ON "categories"."id" = "categorizations"."category_id" WHERE "categorizations"."item_id" = $1 ORDER BY "categories"."id" ASC LIMIT 1 [["item_id", 5]]
CACHE (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
Category Load (0.4ms) SELECT "categories".* FROM "categories" INNER JOIN "categorizations" ON "categories"."id" = "categorizations"."category_id" WHERE "categorizations"."item_id" = $1 ORDER BY "categories"."id" ASC LIMIT 1 [["item_id", 6]]
CACHE (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
Category Load (0.5ms) SELECT "categories".* FROM "categories" INNER JOIN "categorizations" ON "categories"."id" = "categorizations"."category_id" WHERE "categorizations"."item_id" = $1 ORDER BY "categories"."id" ASC LIMIT 1 [["item_id", 7]]
CACHE (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
Category Load (0.4ms) SELECT "categories".* FROM "categories" INNER JOIN "categorizations" ON "categories"."id" = "categorizations"."category_id" WHERE "categorizations"."item_id" = $1 ORDER BY "categories"."id" ASC LIMIT 1 [["item_id", 8]]
CACHE (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
Category Load (0.5ms) SELECT "categories".* FROM "categories" INNER JOIN "categorizations" ON "categories"."id" = "categorizations"."category_id" WHERE "categorizations"."item_id" = $1 ORDER BY "categories"."id" ASC LIMIT 1 [["item_id", 9]]
CACHE (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
Category Load (0.4ms) SELECT "categories".* FROM "categories" INNER JOIN "categorizations" ON "categories"."id" = "categorizations"."category_id" WHERE "categorizations"."item_id" = $1 ORDER BY "categories"."id" ASC LIMIT 1 [["item_id", 10]]
CACHE (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
Category Load (0.4ms) SELECT "categories".* FROM "categories" INNER JOIN "categorizations" ON "categories"."id" = "categorizations"."category_id" WHERE "categorizations"."item_id" = $1 ORDER BY "categories"."id" ASC LIMIT 1 [["item_id", 11]]
Rendered items/index.json.jbuilder (317.5ms)
Completed 500 Internal Server Error in 329ms (ActiveRecord: 11.6ms)
ActionView::Template::Error (undefined method `title' for nil:NilClass):
2: json.array! @items do |item|
3: json.title item.title
4: json.description item.description
5: json.category item.categories.first.title
6: json.price item.price / 100.0
7: json.status item.status
8:
app/views/items/index.json.jbuilder:5:in `block in _app_views_items_index_json_jbuilder___331360250519835275_70269069934300'
app/views/items/index.json.jbuilder:2:in `_app_views_items_index_json_jbuilder___331360250519835275_70269069934300'
答案 0 :(得分:0)
您看到item.categories.first
存在,这确实意味着another_item.categories.first
始终存在且another_item
在循环中是item
:
json.array! @items do |item|
json.category item.categories.first.title
end
请在控制台中查看此内容以查找哪些项目没有任何类别:
Item.all.find_all{ |item| item.categories.empty? }