You are here: Browse Railsplugins Has Many With Params Methods
To help write code with “Fat model, skinny controllers”, it is useful to hide child attributes from Views and Controllers behind parent proxy methods.
i.e. Imagine the simple relation of
class Dog < ActiveRecord::Base
belongs_to :owner
end
class Owner < ActiveRecord::Base
has_many :dogs
end
We can provide fields to insert / update multiple Dogs in a single Owner HTML form:
<input name="owner[name]" type="text" />
<input name="ownerdogs_params[name]" type="text" />
<input name="ownerdogs_params[breed]" type="text" />
<input name="ownerdogs_params[name]" type="text" />
<input name="ownerdogs_params[breed]" type="text" />
Our controller action can continue to be simple (the concept of Dog is hidden).. in fact it can remain unmodified from the vanilla Owner scaffold code:
@owner = Owner.new params[:owner]
@owner.save
This is because, this plugin provides Owner a dogs_params=() method that kicks in when controller does aka,
@owner.name = params:owner @owner.dogs_params = params:ownerAll database queries will happen when @owner is saved and not when @owner.dogs_params is called.
For a more comprehensive example, see test/has_many_with_params_methods_test.rb under “test_this_plugin”
Warning: 3 tables (owners, pets, animals) will be created and dropped from your TEST database.
Just execute this command: PLUGIN=has_many_with_params_methods rake test:plugins
Copyright© 2007 Chew Choon Keat, released under the MIT license
NOTE: This description has been extracted from the Plugin README and so the formatting may need updating to make browser friendly