Attr_accessor for date and issues using select_date

I am trying to use attr_accessor for a date that works fine, except when I try to use it using the select_date helper method.

Looking at the code behind the helper method, I assume that it is looking for a column of a table with a date type. And in this case, since there is no table, it does not process it correctly, and I get:

ActiveRecord :: MultiparameterAssignmentErrors

"search"=>{"number_of_days"=>"3", "searchable_id"=>"6933", "startdate(1i)"=>"2011", "startdate(2i)"=>"2", "startdate(3i)"=>"11"}} 

Is there any way around this? Or do I need to create some kind of filter in front of the controller? I would rather do it at the model level, but I'm not sure how to handle this thing? Attr_accessor for everyone seems a little more killed. Does anyone else have an elegant solution?

+4
source share
3 answers

Attr_accessor fields are usually not saved when saving / updating the model. How do you update the model?

In addition, you can convert the original parameters to a date object as follows:

 @start_date = Date.civil(params[:search][:"startdate(1i)"].to_i,params[:search][:"startdate(2i)"].to_i,params[:search][:"startdate(3i)"].to_i) 

Check here

+3
source

select_date designed to create drop-down lists that are not related to the model field (with the idea that you can pick them up from the other side and do what you want with them). I assume you mean date_select , which runs the model?

In any case, as far as I know, a short story, there is nothing nice and beautiful way to make it work. This is not due to the way the assistant works, but because the active record deals with these attributes, divided into several parameters.

In a little more detail, if you are interested, the reason why this does not work is that when Active Record deals with the parameters that you passed, it goes through execute_callstack_for_multiparameter_attributes , which interprets the keys that were split into the "date" style (1i) ", and redirects them to the appropriate class that they should be (a date or time object), how it works, whether to create a date or time, checking it against the attribute type ( see here ), but since your attribute "startdate" is not bound to a specific type, it is not processed appears as a date or date column in db.

I think I will deal with it just like @ Phyo-Wai-Win, but use select_date to set another parameter outside the search namespace, which you then go into the model as needed in the controller. Thus, it is not so much, and it means that you do not bother with how you initialize the record or what attributes it expects.

0
source

I'm a little late here, but I just ran into this problem and didn't like the main answer. I found a method in the ActiveRecord source called extract_callstack_for_multiparameter_attributes (this is different from the idlefingers method mentioned)

My model has the following method. I call this method manually, but you can probably override update_attributes to automatically run it when saving from the controller. The params argument is actually a parameter [: my_model] from the controller.

 attr_accessor :submit_from, :submit_to def set_dates(params) dates = extract_callstack_for_multiparameter_attributes(params) dates.each_pair do |field, date_array| send "#{field}=", Date.new(*date_array) end end 
0
source

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


All Articles