如何更改记录的类型并使用类型列在 Rails 中运行正确类的验证?
我有一个使用单表继承并具有不同类型(学年类型为学期或季度)的模型,每种类型都有自己的验证。如果用户尝试更改记录类型,他可以从下拉列表中选择学年类型并进行更改。但是,如果用户更改类型,我无法弄清楚如何运行新的类验证而不是旧的验证。例如,我的代码如下:
@school_year = SchoolYear.find(params[:id])
respond_to do |format|
if SchoolYear::SUBCLASSES.include? params[:school_year]['type']
@school_year[:type] = params[:school_year]['type']
else
raise "Invalid type"
end
if @school_year.update_attributes(params[:school_year])
# done
else
# validation problem?
end
现在,如果年份的类型是学期,并且用户将其更改为季度,我希望运行 QuarterSchoolYear 的验证,而不是学期的验证。我如何更改代码以使其正常工作?
I have a model which uses Single table inheritance and has different types (school year of type either semester or quarter) and each type has its own validations. If a user tries to change the type of the record, he can select which academic year type it is from a drop down and make changes. however, if the user changes the type, i cannot figure out how to make new class validations run and not old validations. For instance, my code is as follows:
@school_year = SchoolYear.find(params[:id])
respond_to do |format|
if SchoolYear::SUBCLASSES.include? params[:school_year]['type']
@school_year[:type] = params[:school_year]['type']
else
raise "Invalid type"
end
if @school_year.update_attributes(params[:school_year])
# done
else
# validation problem?
end
now if the year's type was semester, and the user changes it to quarter, i expect the QuarterSchoolYear's validations to run and not of those of semester. How do i make changes to code to make it work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是旧的,但是根据@Arsen7的建议进行清理和改进,将对象重新加载为其新类型的最佳方法是使用以下 ActiveRecord 方法(正是出于这个原因),并且它不需要您更改类型后保存对象。这样,您就不会访问数据库两次:
This is old, but cleaning up and improving upon @Arsen7's suggestion, the best way to reload your object as its new type is to use the following ActiveRecord method (which is made for this very reason), and it doesn't require you to save the object after changing its type. This way, you're not hitting the database twice:
我想你应该在更改类型后重新加载对象。仅向“type”属性分配新值不会更改对象的 ruby 类。当然,当您在类型更改后保存对象时,将使用旧的验证。
您可以尝试更新数据库中的类型属性,然后加载该对象。
类似于:
确保在此类的 attr_accessible 中不包含 :type 。
除此之外,我发现改变对象的类型有点令人不安,但这可能只是我个人的恐惧。 :)
I guess you should reload the object after you change the type. Just assigning a new value to the 'type' attribute will not change the ruby class of the object. Of course, when you save the object just after the type change, the old validations will be used.
You may try to update the type attribute in the database, and then load the object.
Something like:
Be sure to have :type NOT in attr_accessible for this class.
Besides that, I find it a little disturbing to change the type of the object, but that may be just my personal fear. :)
您需要使用正确类型的模型进行实例化才能运行验证。除了使用新类型保存并再次找到它之外,我没有找到其他方法:
您可能希望将其放入事务中,这样您就可以在
update_attributes
的情况下回滚类型的更改失败。You need to instantiate with the right type of model to run the validations. I don't see a way to do this except to save with the new type and find it again:
You'd probably want to put this in a transaction so you could rollback the change of type if
update_attributes
fails.