Perhaps unrelated, but from this question you can come here to see how to do something a little different.
Suppose you want to make Book.new(name: 'FooBar', author: 'SO') , and you want to break some metadata into a separate model called readable_config , which is polymorphic and stores name and author for several models.
How do you take Book.new(name: 'FooBar', author: 'SO') to build the Book model, as well as the readable_config model (which I might mistakenly call a "nested resource")
This can be done like this:
class Book < ActiveRecord::Base has_one :readable_config, dependent: :destroy, autosave: true, validate: true delegate: :name, :name=, :author, :author=, :to => :readable_config def readable_config super ? super : build_readable_config end end
xxjjnn Nov 20 '15 at 10:16 2015-11-20 10:16
source share