I'm trying to use accepts_nested_attributes_for as described at http://archives.ryandaigle.com/articles/2009/2/1/what-s-new-in-edge-rails-nested-attributes
I assumed that second block of code in the tutorial is meant to be in the model, since he says later to do nothing to the controller. The scope seems to signal controller code, though. I have added the following code to the model for "scan", which is supposed to generate child "hostScan" objects before creation of a scan
class Scan < ActiveRecord::Base
attr_accessible :description, :endTime, :startTime, :raw
has_many :hostScans, dependent: :destroy
accepts_nested_attributes_for :hostScans, :allow_destroy => true
before_create :interpret
def interpret
#parse the start and end times of the scan
self.startTime = raw.split(/(?<=timestamps\|\|\|scan_start\|)(.*?)(?=\|)/)[1]
self.endTime = raw.split(/(?<=timestamps\|\|\|scan_end\|)(.*?)(?=\|)/)[1]
#host scan bodies
#host name
#hostScans = raw.scan(/(?<=timestamps\|\|)(.*?)(?=\|host_start)/)
#self.HostScans_attributes = [{}]
#raw host text
hostScanBodies = raw.split(/(?<=host_start\|)(.*?)(?=host_end)/)
hostScanBodies.each do |body|
self.HostScans_attributes += [{:raw => body}]
end
end
end
However, when I try to create a scan, I get the following error:
NoMethodError in ScansController#create
undefined method `HostScans_attributes' for #<Scan:0x2441e68>
It doesn't seem to know about HostScans_attributes.