2

I'm new to ruby on rails.I faced to a situation which i need to get the latest created object from database table.

Currently if there are 2 records for same id it gives me both objects.Finally i get a reports of all.Then the date was printed on two columns.How to get the last created to my report?

 if ticket.user_ticket_actions.present?
       ticket.user_ticket_actions.each do |user_ticket_action|
          if user_ticket_action.act_quality_control.try(:approved) == false 
             ticket_content << user_ticket_action.act_quality_control.created_at.to_datetime.strftime("#{INOCRM_CONFIG['short_date_format']} #{INOCRM_CONFIG['time_format']}")
          end
       end
 end

According to code if there are two act_quality_control days, i need to get the latest one only

Anuja
  • 51
  • 11
  • Can you please associations in question? – Vishal Sep 25 '19 at 06:11
  • @Vishal I posted the association on comments in below answer – Anuja Sep 25 '19 at 08:15
  • If you have two records with the same id, this is a problem that needs to be addressed. ActiveRecord automatically increments id, and it is a bad idea to set id manually. There are several other 'noob' issues with your code. Instead of `if x == false` you should write `unless x`. You should move `created_at.to_datetime...` into the ActQualityControl model, as (e.g.) a `to_s` method, and include in that method the test for `approved`. This will clean up your code and make it easier to read and maintain. – Les Nightingill Sep 25 '19 at 13:09
  • This question is really not a duplicate, OP is asking the wrong question based on a mistake made in the code. The fact that there are multiple models with the same id is the underlying problem. – Les Nightingill Sep 25 '19 at 13:13

0 Answers0