[LRUG] Need advice on creating Model Type A with same data from Model type B

Jesse Waites jesse.waites at gmail.com
Wed Mar 8 05:10:13 PST 2017


Hi all--

I have been tasked with building a report card feature. The end goal of
this is that the user gets a monthly email with various statistics (Users
logged in, Sites activated, things like hat)

I have settled on Report Stat model and Report Card models. One a month,
Report Stat will run and save that data. I take the last 2 report stats (or
any 2 report stats, really - thats why I built it this way) and generate
the Report Card.

The thing is, most of the data from the Report Card will be the same data
from the most recent Report Stat. The users_logged_in from the most recent
Report Stat scan will be the number of users_logged_in in the Report Card.
There are a few instances where I will want a percentage increase or
decrease between Report Stats, I have a integer column ready for that and
will just perform simple math on :users_logged_in between my various time
frames via various Report Stats.

My issue is, how can I most easily transplant the data from ReportStat into
the ReportCard. I could so it manually column by column but that seems like
the wrong way to do it, I want to do it programmatically. The columns and
datatypes are all the same.

At the end of the day, I think I'd like to be able to do
ReportCard.create(:user_id => current_user.id, 6, 7)

with 6 and 7 being the ID number of the reports I want comparisons from.




I am trying it this way but am getting "wrong number of arguments:




class ReportCard < ActiveRecord::Base
  belongs_to :user

  attr_accessible :user_id, :oldest_report_stat_id, :newest_report_stat_id,
:num_active_and_licensed_aeds,
  :num_active_sites, :num_users_logged_in, :aeds_total, :aeds_compliant,
:aed_sites_total,
  :aed_sites_compliant, :responders_total, :responders_compliant,
:num_active_and_licensed_aeds_percentage,
  :num_active_sites_percentage, :num_users_logged_in_percentage,
:aeds_compliant_percentage,
  :aed_site_compliant_percentage, :responders_compliant_percentage,
:report_start_time, :report_end_time



  def execute
    @newest_stat = ReportStat.find(self.newest_report_stat_id)
    @oldest_stat = ReportStat.find(self.oldest_report_stat_id)
    #@newest_stat.inspect
    @newest_stat.assign_attributes.each do |attr_name, attr_value|
      self.send("#{attr_name}=", "#{attr_value}")
    end
    # calculate_percentage_change
  end

end

Here is my rails console output:

r = ReportCard.new
=> #<ReportCard id: nil, user_id: nil, created_at: nil, updated_at:
nil, oldest_report_stat_id: nil, newest_report_stat_id: nil,
num_active_and_licensed_aeds: nil, num_active_sites: nil,
num_users_logged_in: nil, aeds_total: nil, aeds_compliant: nil,
aed_sites_total: nil, aed_sites_compliant: nil, responders_total: nil,
responders_compliant: nil, num_active_and_licensed_aeds_percentage:
nil, num_active_sites_percentage: nil, num_users_logged_in_percentage:
nil, aeds_compliant_percentage: nil, aed_site_compliant_percentage:
nil, responders_compliant_percentage: nil, report_start_time: nil,
report_end_time: nil>
[2] pry(main)> r.oldest_report_stat_id = 6
=> 6
[3] pry(main)> r.newest_report_stat_id = 7
=> 7
[4] pry(main)> r.execute
  ReportStat Load (1.3ms)  SELECT "report_stats".* FROM "report_stats"
WHERE "report_stats"."id" = $1 LIMIT 1  [["id", 7]]
  ReportStat Load (0.4ms)  SELECT "report_stats".* FROM "report_stats"
WHERE "report_stats"."id" = $1 LIMIT 1  [["id", 6]]
ArgumentError: wrong number of arguments (0 for 1..2)
from /Users/enpro/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activerecord-3.2.17/lib/active_record/attribute_assignment.rb:66:in
`assign_attributes'



Any ideas? Maybe syntax error? Ive dumped way too much time into this and
need to move on to the rest of the feature. Thank you LRUG!




-- 
Jesse Waites
Web Developer
JesseWaites.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lrug.org/pipermail/chat-lrug.org/attachments/20170308/5554a2de/attachment.html>


More information about the Chat mailing list