Pastie now auto-senses if line-wrap is a bad or good idea. Feedback?
## mark a section (Learn more)
require 'dm-validations' require 'dm-types' class Subject include DataMapper::Resource property :id, Integer, :serial => true property :name, String, :nullable => false has n, :enrollments has n, :students, :through => :enrollments, :class_name => 'Student' end class Student include DataMapper::Resource property :id, Integer, :serial => true property :name, String property :email, String has n, :enrollments has n, :subjects, :through => :enrollments, :class_name => 'Subject' validates_present :name validates_format :email, :as => :email_address end class Enrollment include DataMapper::Resource property :id, Integer, :serial => true property :subject_id, Integer, :index => true property :student_id, Integer, :index => true belongs_to :subject belongs_to :student end >> subject = Subject.new :name => 'CS101' => #<Subject id=nil name="CS101"> >> subject.save => true >> student = Student.new :name => 'Bill', :email => 'bill@foo.com' => #<Student id=nil name="Bill" email="bill@foo.com"> >> student.save => true >> enrollment = Enrollment.new :subject => subject, :student => student => #<Enrollment id=nil subject_id=1 student_id=1> >> student.enrollments <<<<< this causes the problem => [] >> enrollment.save => true >> student.enrollments => [] >> student.enrollments.all => [] >> student2 = Student.first => #<Student id=1 name="Bill" email="bill@foo.com"> >> student => #<Student id=1 name="Bill" email="bill@foo.com"> >> student == student2 => true >> student.enrollments => [] >> student.enrollments.all => [] >> student2.enrollments.all => [#<Enrollment id=1 subject_id=1 student_id=1>] >>
This paste will be private.
From the Design Piracy series on my blog: