Created
April 12, 2012 03:11
-
-
Save joshmcarthur/2364410 to your computer and use it in GitHub Desktop.
A Nicer Way of Building Records for accepts_nested_attributes_for
This file contains hidden or 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
class YourModel < ActiveRecord::Base | |
has_one :listing | |
accepts_nested_attributes_for :listing | |
attr_accessible :listing_attributes | |
# Make sure we always have a listing | |
after_initialize :build_listing, :unless => :listing | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've seen various implementations of this before - normally in the controller, or using an after_initialize with a another method. This way is nice because:
Listing
accepts_nested_attributes_for