0

I am attempting to get the 'title' from within an array called 'itemType'. 'itemType' is an attribute inside my model 'Item'.

ie: Item { attributes: { name, id, itemType: { title, icon } }

I have been able to get what I want, but only for a single item. I can not in any way loop through the items to access the whole database at once and get the title for each individual item. Ruby yells at me with :

undefined method `[]' for nil:NilClass

So far I have::

<% i = 0 %>
<% len = Item.all.length %>
<% while i < len do %>
    <% items = Item.include_object(:itemType)[i] %>
    <div class="iso_holder">
       <%= item.attributes['itemType']['title']%>
    </div>
    <% i += 1 %>
<% end %>

Any help would be appreciated :)

UPDATE:

Item Model:

class Item < ParseResource::Base
  fields :objectId, :itemType, :user, :createdAt, :updatedAt, :ACL
  validates_presence_of :user

  belongs_to :user
end
3
  • What call to [] is Ruby unhappy with? The call on line #4 or #6? Also, what's include_object? Commented Aug 17, 2012 at 19:20
  • Can you post your item.rb code? You're description of the Item model isn't clear. Commented Aug 17, 2012 at 19:22
  • It's mad about the second []. The first one prints out just fine when looping through all the items. Commented Aug 17, 2012 at 19:26

1 Answer 1

1

your code seems not to use the power or ruby

This should help.

<% Item.all.each do |item| %>
    <div class="iso_holder">
       <%= item.itemType.title unless item.itemType.nil? %>
    </div>
<% end %>

My Probable guess is you are getting itemType as nil somewhere in some record

also have a short look at loops in ruby

I personally feel its not worth using ruby if you are writing the same way you code in other langs :)

Sign up to request clarification or add additional context in comments.

6 Comments

This also throws a similar error message unfortunately. Ruby still doesn't like those brackets for some reason. [undefined method `[]' for #<ItemType:0x30a58d0>]
It takes an extremely long time to actually load, but it works :) Thank you!
maybe due to some reasons its taking time, but this is the proper way to use loops with objects there are others ways too check them out
p.s. The only reason that while loop was being used at all is because I had tried writing this specific part of the code every way I could think of and nothing worked. I'm not really sure how this is much different than the very first way I wrote it. I'm new to Ruby though - little things do make quite the difference.
@PriteshJ - I have to agree. Leigha, you should work through some Ruby and Ruby on Rails tutorials. You'll find that you can accomplish a great deal with very little code once you get the swing of it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.