Search code examples
rubyrubymotion

Rubymotion query using Nano-Store In Motion


I have a method in a iOS application that is querying a database and then putting the data in a ruby loop. I have the loop working to dynamically add a number to the ruby loop but it is pulling up all the users.

# Query all children in database

child = Child.all

# Loop through children and add count.

count = child.count
0.upto(count - 1) do |c|
  p "#{@child.each do |child| child.first_name end}" + "['#{c}']"
end

With this set of query and loop it is giving me the following answers:

"[Child:0x8dee0, Child:0x8de2860]['0']", "[Child:0x8dee0, Child:0x8de2860]['1']"

Instead of giving me the same child for each number I need it to query answers as follows:

"[Child:0x8dee0]['0']", "[Child:0x8de2860]['1']"

Any help would be greatly appreciated. Thank You.


Solution

  • If your loop is trying to print the first names of the children in your array, try this:

    0.upto(count - 1) do |c|
      p child[c].first_name
    end
    

    Alternatively, you could skip the count altogether:

    child.each do |achild|
      p achild.first_name
    end