Accept nested attributes for a record that is not an association

Updated . Posted . Visible to the public.

Note: Instead of using the method in this card, you probably want to use ActiveType's nested attributes Show archive.org snapshot which is a much more refined way of doing this.


The attached Modularity Show archive.org snapshot trait allows you to accept nested attributes for a record that is not an association, e.g.:

    class Site < ActiveRecord::Base
      
      def home_page
        @home_page ||= Page.find_by_name('home')
      end
      
      does 'accept_nested_attributes_for_member', :home_page
      
    end

It has some limitations:

  • Only works for singular member references ("belongs to"), not for a collection of references ("has many")
  • No support for deletion (but could be patched in)
  • You need to build this record yourself (obviously)
Henning Koch
Last edit
Felix Eschey
Keywords
activerecord, rails
License
Source code in this card is licensed under the MIT License.
Posted by Henning Koch to makandra dev (2012-08-30 14:59)