Skip to content
A DataMapper plugin that allows nested model assignment like activerecord.
Ruby
Find file
Latest commit 4efcf10 Jan 31, 2013 @snusnu Merge pull request #20 from postmodern/dependency_bump
Dependency bump
Failed to load latest commit information.
lib Fixed failure to typecast associated keys when updating a one-to-one … Aug 2, 2011
spec
tasks Updated Rakefile, tasks and gemspec to modern DM1 standards. Jan 29, 2013
.document Updated Rakefile, tasks and gemspec to modern DM1 standards. Jan 30, 2013
.gitignore Ignore yard output. Mar 8, 2011
.yardopts Updated Rakefile, tasks and gemspec to modern DM1 standards. Jan 30, 2013
Gemfile Updated the Gemfile. Jan 29, 2013
LICENSE.txt Updated Rakefile, tasks and gemspec to modern DM1 standards. Jan 30, 2013
README.md Updated Rakefile, tasks and gemspec to modern DM1 standards. Jan 30, 2013
Rakefile Updated Rakefile, tasks and gemspec to modern DM1 standards. Jan 30, 2013
dm-accepts_nested_attributes.gemspec Loosen or update dependency requirements. Jan 29, 2013

README.md

dm-accepts_nested_attributes

A DataMapper plugin that allows nested model attribute assignment like activerecord does.

Current documentation can always be found at rdoc.info

Examples

The following example illustrates the use of this plugin.

require "rubygems"

require "dm-core"
require "dm-validations"
require "dm-accepts_nested_attributes"

DataMapper::Logger.new(STDOUT, :debug)  
DataMapper.setup(:default, 'sqlite3::memory:')

class Person
  include DataMapper::Resource
  property :id,   Serial
  property :name, String
  has 1, :profile
  has n, :project_memberships
  has n, :projects, :through => :project_memberships

  accepts_nested_attributes_for :profile
  accepts_nested_attributes_for :projects

  # adds the following instance methods
  # #profile_attributes=
  # #profile_attributes
  # #projects_attributes=
  # #projects_attributes
end

class Profile
  include DataMapper::Resource
  property :id,      Serial
  property :person_id, Integer
  belongs_to :person

  accepts_nested_attributes_for :person

  # adds the following instance methods
  # #person_attributes=
  # #person_attributes
end

class Project
  include DataMapper::Resource
  property :id, Serial
  has n, :tasks
  has n, :project_memberships
  has n, :people, :through => :project_memberships

  accepts_nested_attributes_for :tasks
  accepts_nested_attributes_for :people

  # adds the following instance methods
  # #tasks_attributes=
  # #tasks_attributes
  # #people_attributes=
  # #people_attributes
end

class ProjectMembership
  include DataMapper::Resource
  property :id,         Serial
  property :person_id,  Integer
  property :project_id, Integer
  belongs_to :person
  belongs_to :project
end

class Task
  include DataMapper::Resource
  property :id,         Serial
  property :project_id, Integer
  belongs_to :project
end

DataMapper.auto_migrate!

TODO

  • collect validation errors from related resources
  • update README to include more complete usecases
  • think about replacing :reject_if with :if and :unless
Something went wrong with that request. Please try again.