On 4 February 2016 at 15:30, John Sanderbeck <lists@ruby-forum.com> wrote:
> I am new to Rails and Ruby and am having a difficult time building the
> data needed for a Highchart. I am struggling with the syntax to map a
> pair of hashes to an array. Let me try and explain:
>
> I have a table of "initiatives" which contains basically an ID and a
> Name.
>
> Then I have a table "trainings" that has the initiative_id field to map
> to the initiative table
>
> What I am trying to do is create a method that pulls the Initiative name
> and the count of records from the trainings, and returns an array with
> [[name,count],[name,count]]
>
> What I have been trying is this
>
> def training_count_by_initiative()
> initiative_name = Initiative.all.select("id,name")
> training_count = Training.group(:initiative_id).count
> initiative_name.map do |initiative|
> ??? This is the part I am struggling with
> end
> end
>
> I tried following Railscasts #223 but can't seem to get the mapping to
> work.
>
> I know it is just a Ruby syntax issue that I am not understanding so if
> someone can explain it to me I would greatly appreciate it.
You don't need the first two lines, you just need something like
Initiative.all.map {|initiative| [initiative.name, initiative.trainings.count] }
Colin
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLvX0xJu5_vR8VZ8tYvxy57n_TiRaAfpB2rVV7nN8MWZFw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment