Rails ActiveRecord :: MultiparameterAssignmentErrors

My model has the following code:

attr_accessor :expiry_date validates_presence_of :expiry_date, :on => :create, :message => "can't be blank" 

and in my opinion:

 <%= date_select :account, :expiry_date, :discard_day => true, :start_year => Time.now.year, :end_year => Time.now.year + 15, :order => [:month, :year] %> 

However, when I submit my form, I get:

 ActiveRecord::MultiparameterAssignmentErrors in SignupController#create /Users/x/.rvm/gems/ruby-1.8.6-p383/gems/activerecord-2.3.5/lib/active_record/base.rb:3073:in `execute_callstack_for_multiparameter_attributes' /Users/x/.rvm/gems/ruby-1.8.6-p383/gems/activerecord-2.3.5/lib/active_record/base.rb:3028:in `assign_multiparameter_attributes' /Users/x/.rvm/gems/ruby-1.8.6-p383/gems/activerecord-2.3.5/lib/active_record/base.rb:2750:in `attributes=' /Users/x/.rvm/gems/ruby-1.8.6-p383/gems/activerecord-2.3.5/lib/active_record/base.rb:2438:in `initialize' 

Any ideas on what might be the issue? I looked at # 93277 without joy, so I'm stuck.

Adding a day to the selection does not resolve the problem.

Ultimately, what I'm trying to accomplish is a property of a model that is not stored in the database, but verified. This already works for some other simple string fields in the same model, not: expiry_date

Any ideas?

+3
source share
2 answers

If you use attr_accessor, it means that you do not save this field in the database.

The problem remains that you cannot use the attribute of a non-constant model (i.e. actually not be stored in the database through the model) with helpers.

Why Rails 3 has an ActiveModel: to use an object, enable some ActiveModel behavior (through the inclusion of the module) and use it with the ActionPack helpers (if I understood everything well :)).

Try replacing attr_accessor with attr_accessible or even drop this line if you want to protect this field from mass assignment.

Hope this helps.

0
source

According to https://github.com/rails/rails/blob/v3.0.4/activerecord/lib/active_record/base.rb#L1764 Rails will ask the class what type is for this column. Since this attribute is not a column, we get nil, and nil does not have a klass class. So, I just paid column_for_attribute . I put this in my class (my attribute was birth_date ):

 def column_for_attribute_with_birth_date(name) if name == 'birth_date' return Object.new.tap do |o| def o.klass Date end end end column_for_attribute_without_birth_date(name) end alias_method_chain :column_for_attribute, :birth_date 
+1
source

Source: https://habr.com/ru/post/1345598/


All Articles