开发者

Looping the records for a checkbox

开发者 https://www.devze.com 2023-01-19 17:49 出处:网络
I have this data and as you can see I have about 10 requests. A request has a venue, song, date and artist. I need to loop those requests and if there is are two request with the same song made to the

I have this data and as you can see I have about 10 requests. A request has a venue, song, date and artist. I need to loop those requests and if there is are two request with the same song made to the same venue and date then i need to add then to a checkbox. For example:

#<Request id: 8, artist: "Metallica", song: "Turn the Page", venue: 
"Rod Laver Arena at 开发者_开发问答Melbourne Park - Melbourne Vic", showdate: "2010-10-09", 
amount: nil, user_id: 6, created_at: "2010-10-09 01:32:26", 
updated_at: "2010-04-09 01:32:26", like_reason: "Its a good song", pledge: 100>,

#<Request id: 9, artist: "Metallica", song: "Turn the Page", venue: 
"Rod Laver Arena at Melbourne Park - Melbourne Vic", showdate: "2010-10-09", 
amount: nil, user_id: 6, created_at: "2010-10-09 01:32:26", 
updated_at: "2010-05-09 01:32:26", like_reason: "Its great", pledge: nil>,

#<Request id: 10, artist: "Metallica", song: "Enter Sandman", venue: 
"Aector Arena - Auckland Central, New Zealand", showdate: "2010-10-11", 
amount: nil, user_id: 6, created_at: "2010-10-09 01:32:26", 
updated_at: "2010-05-09 01:32:26", like_reason: "good tune", pledge: nil>,

As you can see i have two request to the same venue and the same date and the same song. I want a loop that will create checkboxes in the view but if it encounters the same song with the same date info then add the id in the checkbox array..like this

<input value="Turn the Page"  name="song[8, 9]" type="checkbox">

other wise i would only have one in the array for that checkbox is that is the only one

<input value="Enter Sandman"  name="song[10]" type="checkbox">

@past_requests is the array that will contain all the requests... Any ideas on how to achieve this


If you can come up with a group_by function that groups by the combination of venue, date and song then that would simplify the problem.

e.g. as a simple example we could use a concatenated string as a the key:

grouped_requests = requests.group_by do |req|
  "#{req.artist}/#{req.song}/#{req.venue}/#{req.date}"
end

Then your checkboxes could be generated with:

<% grouped_requests.values.each do |requests| %>
  <% request_ids = requests.collect { |request| request.id } %>
  <%= check_box_tag requests.first.song, "song[#{request_ids.join(',')}]" %>
<% end %>

Notice that we don't care which request we use for the song name because all the requests in a grouping have the same song so we can just use requests.first.song

Alternatively, you could look at extracting venue/date/song into a Performance model and then linking requests to performances. That might simplify things.


Use the Rails/ActiveSupport group_by method

@past_requests.group_by(&song_id).each do |song_id, requests|
  ... #create the checkbox tag here
end

Things to consider

  • I created the song_id attribute, I couldn't tell what 8,9,10 were
  • This seems a weird way for you to consolidate everything down and group by song.
0

精彩评论

暂无评论...
验证码 换一张
取 消