I think I've needed and implemented this twice now, so let's make a post about it.
The problem occurs when you are using f.fields_for and nested forms, as per these railscasts, and the nested objects are using STI.
(Will leave a more detailed explanation for future posts).
Update 1: Even though this works as described, I don't think it solves my problem. Will post again if I figure it out...
Update 2: Found this which is I think what I was using last time I had this problem... :S
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# This was causing problems when submitting the nested object form | |
# in a/v/product/channels_edit because all the nested channels would | |
# be created at plan Channel objects instead of the PrimaryChannel, EusChannel etc. | |
# | |
# In active record normally :type is ignored, eg: | |
# Channel.create(:type='PrimaryChannel', :etc=>1, ...) | |
# ignores the type attr and uses 'Channel'. | |
# | |
# This hack will make it actually use the :type value. | |
# | |
# Usage: | |
# | |
# class User < ActiveRecord::Base | |
# extend ActiveRecord::CreateWithSubtype | |
# ... | |
# end | |
# | |
# class AdminUser < User | |
# ... | |
# end | |
# | |
# >> User.create(:type=>'AdminUser', :username=>'simon', ...) #=> #<AdminUser id:123 ...> | |
# | |
# Actually needed this for nested objects in forms using accepts_nested_attributes_for. | |
# http://railscasts.com/episodes/197-nested-model-form-part-2 | |
# | |
# When updating the type is preserved, but when creating a new subobject with the nested form | |
# the type is always the base class, which in my case was a problem. | |
# | |
# (I put this file in lib/active_record/create_with_subtype.rb) | |
# | |
module ActiveRecord | |
module CreateWithSubtype | |
def create(attributes=nil, &block) | |
# Check if :type attribute exists. Remove it from attributes | |
# (Note that attributes can be an array, hence Hash test) | |
specified_type = attributes.delete(:type) if attributes.is_a?(Hash) | |
if specified_type | |
# Find the specified class | |
use_sub_class = const_get(specified_type) | |
# Sanity check | |
raise "'#{use_sub_class}' not a subclass of '#{self}'" unless self.descendants.include?(use_sub_class) | |
# Call create on the specified subclass | |
use_sub_class.create(attributes, &block) | |
else | |
# Otherwise, just do the normal thing | |
super(attributes, &block) | |
end | |
end | |
end | |
end |
No comments:
Post a Comment