Question

I'm writing to an email table every-time my app receives emails.

I'm passing in an email object to create the record, but failing to figure out how to translate the email.to property, which is an array of hashes.

I have the corresponding EmailAddress has many model set up with the proper attributes to transfer over!

The Email model has_many :email_addresses, class_name: "EmailAddress", as: "email_addressable".

class EmailProcessor
  def self.process(email)
    Email.create!({ 
                    body:       email.body,
                    subject:    email.subject,
                    email_addresses: email.to
                  })
  end
end

An example of what I beleive the email.to would look like:

email.to = [{
  :token => "A",
  :email => "A2",
  :full  => "A3",
  :name  => "A4"
},{
  :token => "B",
  :email => "B2",
  :full  => "B3",
  :name  => "B4"
}]
Was it helpful?

Solution

You should add accepts_nested_attributes_for :email_addresses within your Email model if you want to be able to create a new Email together with its associated new EmailAddresses using nested attributes. You could learn more from this StackOverflow answer: How is attr_accessible used in Rails 4?

I'm assuming you're using Rails 4.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top