[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 06:20:11 PST 2017


@Duncan:

"Is there a reason that ReportCards need to be saved to the database? Would
it make sense to instead generate them 'on-demand' based on the data from
ReportStats?"

I had the same thought, but am told that there is a 99% chance our users
will want to see these Report Cards in the web app at some point, so makes
sense to just make active record objects out of them, so they can be scoped
old to new, new to old, etc. Good thought, though, I was leaning the same
way initially.

@Toby:

You are awesome, your advice got me going in the right direction. I am now
at:

r.assign_attributes(@newest_stat.attributes)
ActiveModel::MassAssignmentSecurity::Error: Can't mass-assign
protected attributes: id, created_at, updated_at


Just got that error though, havent even started looking up how to fix
it yet but Im sure Ill come up with something!

Maybe make a new object and strip those from it before calling
.attrributes or stripping them from Report Stat first.



On Wed, Mar 8, 2017 at 9:12 AM, Toby Privett <tobyprivett at gmail.com> wrote:

> Correction: "I think you are looking for *something that can iterate*"
>
> On Wed, 8 Mar 2017 at 14:10 Toby Privett <tobyprivett at gmail.com> wrote:
>
>> #assign_attributes expects a hash http://apidock.com/rails/
>> v4.2.7/ActiveRecord/AttributeAssignment/assign_attributes
>>
>> I think you are looking for an iterator
>> e.g. @newest_stat.attributes
>>
>> Hope that helps
>>
>> On Wed, 8 Mar 2017 at 14:01 Jesse Waites <jesse.waites at gmail.com> wrote:
>>
>>> 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
>>> _______________________________________________
>>> Chat mailing list
>>> Chat at lists.lrug.org
>>> Archives: http://lists.lrug.org/pipermail/chat-lrug.org
>>> Manage your subscription: http://lists.lrug.org/options.
>>> cgi/chat-lrug.org
>>> List info: http://lists.lrug.org/listinfo.cgi/chat-lrug.org
>>>
>>
> _______________________________________________
> Chat mailing list
> Chat at lists.lrug.org
> Archives: http://lists.lrug.org/pipermail/chat-lrug.org
> Manage your subscription: http://lists.lrug.org/options.cgi/chat-lrug.org
> List info: http://lists.lrug.org/listinfo.cgi/chat-lrug.org
>
>


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


More information about the Chat mailing list