Hello,

I am trying to insert elements into an array like this...

@mem = []@tran = Transport.find_all_by_month_and_vehicle(date,vehicle)tran.each do |t|
   @mem << [Student.find_by_id(t.mem_id), t.transport_date, vehicle.no] if t.mem_type=="Student"
   @mem << [Employee.find_by_id(t.mem_id), t.transport_date, vehicle.no] if t.mem_type=="Employee"end

And in the view page I am looping and displaying it as

@mem.each do |m|
   <tr>
             <td><%= link_to m[0].first_name} %></td>
             <td > <%= m[0].age %></td>
             <td id="date"> m[1] </td>
             <td id="vehicle"> m[2] </td>
   </tr><%end%>

But I am getting duplicate entries in the table... So I neeed to remove duplicates and display only unique values.. I tries doing
   @mem << [Student.find_by_id(t.mem_id), t.transport_date, vehicle.no].uniq! if t.mem_type=="Student".. But it is not working.. Please help


--
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/bb65bdc3-1a10-4139-9c98-103b5dddda4b%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Crispin Schäffler at Sep 9, 2013 at 8:17 am
    @tran = Transport.where( date: date, vehicle: vehicle)
    @mem = @tran.members

    Iterate in view

    @mem.each do |mem|
       mem.name
       mem.vehicle

    Should work if you have the relations set up. You should go read one or two tutorials about relations and how to set them up.

    --
    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/f89e14d1-73f9-4482-bb5a-7bd45b6654ea%40googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Max at Sep 9, 2013 at 12:33 pm
    if you really need to do some processing that gets you an array with
    duplicate entries and then clean the array at the end of it all. try doing
    something like

    @mem = @mem.uniq

    you can probably do @mem.uniq! but i've had some cases where that didn't
    get me the results i wanted.

    --
    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/cced12b8-9434-41f5-8a51-2f643f450973%40googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Joel Pearson at Sep 9, 2013 at 9:29 pm
    uniq can take a block as well if you wanted to unique similar arrays on
    a specific element:

    @mem.uniq { |mem| mem[1] }

    Or even on multiple criteria:

    @mem.uniq { |mem| [ mem[1], mem[2] ] }

    --
    Posted via http://www.ruby-forum.com/.

    --
    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/a415e1e78635c3b25fb9c233777a5817%40ruby-forum.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Sergei Barilov at Nov 20, 2013 at 5:09 am

    Joel Pearson wrote in post #1121051:
    uniq can take a block as well if you wanted to unique similar arrays on
    a specific element:

    @mem.uniq { |mem| mem[1] }

    Or even on multiple criteria:

    @mem.uniq { |mem| [ mem[1], mem[2] ] }

    This was very helpful

    Thank you

    --
    Posted via http://www.ruby-forum.com/.

    --
    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/34f63f66fbd5dfe641af96058295e436%40ruby-forum.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedSep 8, '13 at 3:39p
activeNov 20, '13 at 5:09a
posts5
users4
websiterubyonrails.org
irc#RubyOnRails

People

Translate

site design / logo © 2022 Grokbase