mardi 21 avril 2015

undefined method `id' for nil:NilClass in Jbuilder when trying to access object's attributes

I'm trying to build up API services in rails and code is

json.array! @available_games do |available_game|
  product = Product.where(process_name: available_game).first
  json.name available_game
  json.product product    
  json.status "Available"
end

it render whole product obejct in json format and works fine.

However, I try to using

json.app_id product.id 
json.app_name product.name

I always got errors "undefined method `id' for nil:NilClass" when I try to access attributes of object. No idea about why I got errors when I try to do this.

Very Thanks.

Aucun commentaire:

Enregistrer un commentaire