0

There are several related questions to this one. But none of the answers address my situation.

I am getting this error on my localhost when trying to create a new policy.

Can't mass-assign protected attributes: starts_on

In my policy.rb model though I have this:

class Policy < ActiveRecord::Base

belongs_to :policy_type

attr_accessible :starts_on,
                :ends_on,
                :i_agree_privacy_policy,
                :license,
                .
                .
                .etc...

validates:starts_on, :presence => true

def self.init(user, policy_type, load_user_profile = true)
    attributes = {
        :user => user,
        :policy_type => policy_type,
        :starts_on => Date.today
    }
    policy_type.policy_class.constantize.new(attributes)
end

etc...
end

The form is for insurance so its huge but its also not important here since I am creating the starts_on here with Date.today. Really stumped here and I've spent several hours on google the last couple of days to figure out why this is happening.

Ryan
  • 5,644
  • 3
  • 38
  • 66
  • I'm curious why you aren't using `Policy.new attributes` in the place of `policy_type.policy_class.constantize.new(attributes)`. What is policy_class returning? – Andrew Kothmann Sep 07 '12 at 21:51
  • I didn't write the line. But policy_class is a column in the policy_types table and it holds a string. I am trying to refactor the whole project actually and it included changing the policies table. but this column `starts_on` was in the old table. and I didn't touch the policy_types table – Ryan Sep 07 '12 at 21:58
  • What type is field 'starts_on' of? – Eru Sep 08 '12 at 01:54

1 Answers1

0

You're probably getting the "Can't mass assign" error on the actual class that policy_type.policy_class resolves to, not on Policy.

I would check whatever class policy_type.policy_class points to, and try adding starts_on to attr_accessible there.

Alistair A. Israel
  • 6,417
  • 1
  • 31
  • 40