2016-07-16 2 views
0

J'utilise jbuilder pour créer une réponse json. Voir ci-dessous, mon modèle de JBuilder:Rails 4, No Method Erreur lors de la tentative d'accès aux attributs d'un objet dans le modèle 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 

Je suis en mesure d'accéder à un objet de la collection Item.Categories comme si, sans problème:

item.categories.first

Lorsque je tente de tirer le titre attribut de l'objet de la catégorie de retour en haut que je reçois cette erreur dans le navigateur:

undefined method 'title' for nil:NilClass

Je mets une déclaration byebug juste avant cette ligne dans mon modèle de JBuilder:

item.categories.first.title

Et dans la console byebug je peux accéder à l'attribut title aucun problème. Voir la console byebug imprimer ci-dessous:

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) 

Je suis perplexe, qu'est-ce que je fais mal?

EDIT:

Voici le sql qui est déclenché lorsque JBuilder rend la vue:

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

Ok, pouvez-vous vérifier le SQL qu'il déclenche lorsque Jbuilder affiche la vue? Que SQL sera le bon point de débogage pour commencer –

+0

Je suis posté le SQL qui est déclenché lorsque jbuilder rend la vue, s'il vous plaît jeter un oeil – matthewalexander

+1

Ok, bien. Je peux voir maintenant il y a des articles avec id * 1 * à * 11 * qui a _categories_. L'article suivant après * id 11 * est en train de casser le code. Maintenant, ouvrez la 'rails console' et l'élément qui a' id' comme '12' probablement, et je suis sûr qu'il n'a aucune catégorie associée. Et cela cause le problème. Donc, vous devez faire un filtrage pour sélectionner uniquement les éléments qui ont des catégories. –

Répondre

0

Vous voyez item.categories.first existe, cela signifie que another_item.categories.first existe toujours avec another_item est un item dans la boucle :

json.array! @items do |item| 
    json.category item.categories.first.title 
end 

Veuillez vérifier dans la console pour trouver les éléments qui n'ont pas de c ategory: Item.all.find_all{ |item| item.categories.empty? }