Represent an array

15 views
Skip to first unread message

Tomás Barrios

unread,
Dec 22, 2014, 3:45:00 PM12/22/14
to roar...@googlegroups.com
Hi,

I am trying to represent an object and its associated objects, the problem is that is not a direct assoc, so i made a method to retrieve the deep query and bring only the info i need

finally the method returns an array which works "almost" as i expected, BUT, the representation adds an extra [ ] around the json.

This is the method.

      property :how_to_get_to_city_center
     
     
def how_to_get_to_city_center
        answers
= []
        transport_stations
.each do |ts|
          ts
.questions.each do |q|
            best
= q.answers.sort_by{|a| a.rates_mean}
            answers
<< best unless best.empty?
         
end
       
end
        answers
     
end


And this is the JSON output (after how_to_get_to_city_center you can see the extra [). Is there a way to do what I am trying? Thanks!

  • {
    id: 597,
    name: "Santiago",
    ...
    updated_at: "2014-12-17T19:03:06Z",
    how_to_get_to_city_center:
    [
    [
    {
    created_at: "2014-12-22T16:15:03Z",
    id: 41,
    question_id: 71999,
    text: "My answer!",
    updated_at: "22 Dec 2014",
    user_id: 18,
    rating: "0.0",
    rates: [ ],
    reviews_count: 0,}]]}

Rhett Sutphin

unread,
Dec 22, 2014, 4:08:46 PM12/22/14
to roar...@googlegroups.com
Hi Tomás,

It looks like your `answers` array is an array of arrays, because `best` in the inner loop is an array. If you want to ensure you get just a one-dimensional array, you can use `answers.flatten` at the end, or use `flat_map` while constructing the result for `how_to_get_to_city_center`. Or, if you expected `best` to be a single value, maybe you wanted to do `q.answers.sort_by { |a| a.rates_mean }.first`.

(BTW, this is more of a general ruby question than something Roar-specific. Roar is accurately serializing the property value you’re giving it.)

Rhett
> --
> You received this message because you are subscribed to the Google Groups "Roar" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to roar-talk+...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Tomás Barrios

unread,
Dec 22, 2014, 4:12:31 PM12/22/14
to roar...@googlegroups.com
Sorry! My mistake.

I thought i was probably doing something wrong, that there was another way to better do this with roar.

Thanks!

You received this message because you are subscribed to a topic in the Google Groups "Roar" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/roar-talk/XjJcBbt1QIQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to roar-talk+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages