86745

Listing objects under the date they were created

Question:

Sorry for the amateur question but I am still quite new to rails. I currently have an app that creates jobs and I would like to display the jobs beneath the date they were created in the same way they do on <a href="http://www.dribbble.com/jobs" rel="nofollow">Dribble</a>

At the moment to display the jobs I have te following:

<% @jobs.each do |job| %> <div class="job-wrapper"><%= link_to user_job_path(job.user_id ,job) do %> <div class="job"> <div class="job-desc"><strong><%= job.company %></strong> are looking for a <strong><%= job.job_title %></strong> <div class="job-sal"><%= job.job_salary %></div> </div> </div> </div> <% end %> <% end %>

I am sure I need to create a loop of some kind to make this work but am unsure how to incorporate it so that the date only displays once at the top of the jobs and then all jobs created during that date are shown?

Any help would be much appreciated! Thanks

Answer1:

Try the pattern below --

<% @job.group_by{|x| x.created_at.to_date }.each do |date,jobs_on_that_date| %> <%= date %> <% jobs_on_that_date.each do |job| %> # render the job <% end %> <% end %>

Basically you need to group your jobs by the date (or whatever you want to group on) then get a hash keyed on the stuff you grouped on. Then render the key (date) followed by the list of objects relating to that key.

Recommend

  • High score and current score
  • wso2 esb connection timeout tuning
  • convert to PROCEDURE pl/sql
  • rails 5.1: nested form validation
  • How to add description lines to the header of a .txt output in R
  • AES_ENCRYPT and AES_DECRYPT using java
  • Parsing XML from web
  • refresh data grid view after updating in vb.net
  • CosmosDB SQL query that matches multiple values in an array
  • Inconsistent datatypes: expected REF got .. error when trying to insert data
  • Generate source using jaxb from an xsd that is in my classpath
  • Creating a Countdown Watch with a slider in Xcode
  • Pick folder routine going to Error handler - Excel VBA
  • Giving right align to numeric data in datatables
  • How to make Rails do not ignore trailing slashes in the routes?
  • Custom routes with rails 3.1
  • How to call java class from oracle trigger?
  • Using Resque and resque_mailer to send emails raises Routing error
  • reduce/reduce conflicts using ocamlyacc
  • Encrypting credit card details using AngularJS in Braintree
  • Autohotkey script running program with command line arguments
  • Table striping rows in CSS Grid
  • ZipList with Scalaz
  • abstracting over a collection
  • How do I get the list of bad records that didn't load in Bigquery?
  • Could not find rake using whenever rails
  • FileReader+canvas image loading problem
  • Q promise. Difference between .when and .then
  • Insert into database using onclick function
  • Deselecting radio buttons while keeping the View Model in synch
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • what is the difference between the asp.net mvc application and asp.net web application
  • Java static initializers and reflection
  • unknown Exception android
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Unable to use reactive element in my shiny app
  • Conditional In-Line CSS for IE and Others?