Read more

Accept nested attributes for a record that is not an association

Henning Koch
August 30, 2012Software engineer at makandra GmbH

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.


Illustration UI/UX Design

UI/UX Design by makandra brand

We make sure that your target audience has the best possible experience with your digital product. You get:

  • Design tailored to your audience
  • Proven processes customized to your needs
  • An expert team of experienced designers
Read more Show archive.org snapshot

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)
Posted by Henning Koch to makandra dev (2012-08-30 16:59)