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 online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
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)