我正在尝试使用 attr_accessor 来获取通常工作正常的日期,除非我尝试将它与 select_date 帮助器方法一起使用。
查看辅助方法背后的代码,我猜测它会查找具有日期类型的表列。在这种情况下,由于没有表,它无法正确处理它,我得到:
ActiveRecord::MultiparameterAssignmentErrors
"search"=>{"number_of_days"=>"3",
"searchable_id"=>"6933",
"startdate(1i)"=>"2011",
"startdate(2i)"=>"2",
"startdate(3i)"=>"11"}}
有没有办法解决这个问题?或者我是否需要在控制器中创建某种之前的过滤器?我更喜欢在模型级别上执行此操作,但我不确定如何处理这种情况?每个的 attr_accessor 似乎有点过头了。还有其他人有优雅的解决方案吗?
I'm trying to use attr_accessor for a date which normally works fine except when I attempt to use it with the select_date helper method.
Looking at the code behind the helper method I'm guessing it looks for the table column with date type. And in this case since there is no table it's not handling 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 a way around this? Or do I need to create some kind of before filter in the controller? I'd prefer doing it on the model level, but I'm not sure how to handle this case? An attr_accessor for each seems a bit over kill. Anyone else have an elegant solution?
发布评论
评论(4)
当您保存/更新模型时,attr_accessor 字段通常不会被保存。您如何更新模型?
另外,您可以将 startdate 参数转换为日期对象,如下所示:
检查此处
attr_accessor fields don't usually get saved when you save/update to the model. How are you updating the model?
Also, you can convert the startdate params to a date object like this :
Check here
select_date
用于构建与模型字段无关的下拉菜单(这样您就可以在另一侧选择它们并对其执行您想要的操作)。我假设您的意思是date_select
,它确实会从模型中运行?无论如何,据我所知,长话短说,没有什么好的方法可以让它发挥作用。这不是因为助手的工作方式,而是因为活动记录处理这些属性(拆分为多个参数)的方式。
在您有兴趣的情况下,更详细地说,这不容易起作用的原因是,当活动记录处理您传递的参数时,它会通过
execute_callstack_for_multiparameter_attributes
解释已拆分为的键“date(1i)”样式,并将它们放入它们应该是的适用类中(日期或时间对象)。确定是否应该创建日期或时间的方法是根据属性的类型进行检查(参见此处),但由于您的“startdate”属性未绑定到特定类型,因此它不会被视为日期或日期时间列在数据库中会。我想我会以类似于 @Phyo-Wai-Win 的方式处理它,但使用
select_date
在“搜索”命名空间之外设置不同的参数,然后根据需要将其传递到模型中控制器。这样,就不需要太多工作,并且意味着您不会弄乱初始化记录的方式或它期望的属性。select_date
is for building the dropdowns which are not associated with a model field (with the idea that you can then pick them up on the other side and do what you want with them). I assume you're meaningdate_select
which does run off the model?In any case, as far as I know, long story short, there's no nice and pretty way to get this to work. It's not because of the way the helper works, but because of the way that active record deals with these attributes split into multiple parameters.
In a bit more detail if you're interested, the reason why this doesn't work easily is because when Active Record is dealing with the params you've passed in, it goes through
execute_callstack_for_multiparameter_attributes
which interprets the keys which have been split into the "date(1i)" style, and mungs them into the applicable class which they should be (a date or time object). The way it works out whether it should create a date or time is by checking it against the type of the attribute (see here), but since an your 'startdate' attribute isn't bound to a particular type, it doesn't get treated as a date or datetime column in the db would.I think I would deal with it similarly to @Phyo-Wai-Win, but use
select_date
to set a different param, outside of the 'search' namespace which you then pass into the model as appropriate in the controller. This way, it's not much work, and it means you're not messing with the way you initialize the record or what attributes it expects.来得太晚了,但以防万一其他人绊倒,现代 Rails 的答案在于
include ActiveRecord::AttributeAssignment
在您的模型中。这个答案为我做到了。
Coming in way late, but in case anyone else stumbles by, the answer for modern rails lies in
include ActiveRecord::AttributeAssignment
in your model.This answer did it for me.
我来晚了一点,但我刚刚遇到这个问题,并且不喜欢最上面的答案。我在 ActiveRecord 源中找到了一个名为 extract_callstack_for_multiparameter_attributes 的方法(这与提到的idlefingers 方法不同),
我的模型中有以下方法。我手动调用此方法,但您可能可以覆盖
update_attributes
以在从控制器保存时自动运行它。 params 参数实际上是来自控制器的 params[:my_model] 。I'm a little late here, but I just came across this problem and did not like the top answer. I found a method in the ActiveRecord source called extract_callstack_for_multiparameter_attributes (this is different than the method idlefingers mentioned)
I have the following method in my model. I am calling this method manually but you could probably override
update_attributes
to run it automatically when you save from the controller. The params argument is actually params[:my_model] from the controller.