https://github.com/airblade/acts_as_parent_of
Implements the parent model code from Advanced Rails Recipe 13.
https://github.com/airblade/acts_as_parent_of
Last synced: 6 months ago
JSON representation
Implements the parent model code from Advanced Rails Recipe 13.
- Host: GitHub
- URL: https://github.com/airblade/acts_as_parent_of
- Owner: airblade
- License: mit
- Created: 2008-10-28T19:46:38.000Z (almost 17 years ago)
- Default Branch: master
- Last Pushed: 2009-09-10T12:21:26.000Z (about 16 years ago)
- Last Synced: 2025-02-06T08:24:43.000Z (8 months ago)
- Language: Ruby
- Homepage:
- Size: 89.8 KB
- Stars: 2
- Watchers: 5
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.markdown
- License: MIT-LICENSE
Awesome Lists containing this project
README
# ActsAsParentOf
If you are on Rails 2.3 or above, use Rails' [nested attributes](http://guides.rubyonrails.org/2_3_release_notes.html#nested-attributes) feature.
If you are on Rails 2.2 or below, keep reading.
-----
This is lifted straight out of Advanced Rails Recipe 13, "Handle Multiple Models In One Form", by Ryan Bates.
For those using the recipe, you can save yourself some typing by adding the `acts_as_parent_of` declaration to your parent model. For example:
class Project < ActiveRecord::Base
acts_as_parent_of :tasks, :budget
endYou'll still need to write your views and controller as explained in the recipe.
## has_many versus has_one
Although the recipe assumes your parent has many children, `acts_as_parent_of` also handles the case where the parent has one child (e.g. `:budget` in the example above). Just remember in your controller's `update` method to set the child's attributes to `nil` rather than `{}`. For example:
class ProjectController < ApplicationController
def update
params[:project][:existing_task_attributes] ||= {}
params[:project][:budget_attributes] ||= nil# your normal update code etc...
end
endCopyright (c) 2008 Andy Stewart (boss@airbladesoftware.com), released under the MIT license