Skip to content

Instantly share code, notes, and snippets.

@emmanuel
Created May 27, 2011 00:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emmanuel/994447 to your computer and use it in GitHub Desktop.
Save emmanuel/994447 to your computer and use it in GitHub Desktop.
Resources created via OneToMany::Collection#new work, but not if conditions are applied
require 'rubygems'
# puts ENV["GEM_HOME"]
require 'ruby-debug'
require 'datamapper'
DataMapper::Logger.new(STDOUT)
DataMapper.setup(:default, "sqlite::memory:")
class Page
include DataMapper::Resource
property :id, Serial
has n, :page_components, :child_key => :page_id
def masthead_left
page_components(:section => "masthead_left")
end
end
class PageComponent
include DataMapper::Resource
property :page_id, Integer, :key => true, :index => :page
property :section, String, :key => true, :length => 32
belongs_to :page, :child_key => :page_id, :inverse => :page_components
def self.masthead_left
all(:section => "masthead_left")
end
end
DataMapper.auto_migrate!
puts '******* Explicit conditions to #new'
page = Page.create
component = page.page_components.new(:section => "masthead_right")
page.save
# components IS in page.components, page.save does cascade:
puts '******* Explicit conditions to association method'
page.reload
collection = page.page_components(:section => "masthead_left")
collection.class # => DataMapper::Associations::OneToMany::Collection
collection.query.conditions.class # => DataMapper::Query::Conditions::AndOperation
component = collection.new
page.save
# components is NOT in page.components, so save doesn't cascade
puts '******* Association-derived collection (#new via Collect#delegate_to_model)'
page.reload
component = page.page_components.masthead_left.new
page.save
# components is NOT in page.components, so save doesn't cascade
puts "*********************************"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment