Skip to content

Instantly share code, notes, and snippets.

@kematzy
Created March 25, 2010 06:44
Show Gist options
  • Select an option

  • Save kematzy/343265 to your computer and use it in GitHub Desktop.

Select an option

Save kematzy/343265 to your computer and use it in GitHub Desktop.
# PROBLEM SYNOPSIS
#
# Updating the has n :faqs association key :faq_category_id inside a before block in the parent modeL
# DOES NOT WORK
#
# It does however work outside of the before block.
#
#
%w( rubygems dm-core dm-validations dm-timestamps dm-types dm-is-list dm-is-tree ).each { |lib| require lib }
class FaqCategory
include DataMapper::Resource
property :id, Serial, :key => true
property :parent_id, Integer, :required => true
property :name, String, :length => 100, :required => true
has n, :faqs
# is :list, :scope => [:parent_id], :order => [:position]
# is :tree, :order => [:parent_id, :position]
before :destroy do
# WORKING CODE
# faqs.map(&:id).each do |f|
# Faq.get(f).update(:faq_category_id => self.parent_id)
# end
# throws [ DataMapper::UpdateConflictError: Faq#update cannot be called on a dirty resource " ]
# faqs.update(:faq_category_id => parent_id)
## doesn't update the faq_category_id on save
faqs.each do |faq|
faq.faq_category_id = parent_id
puts faq.errors.inspect
faq.save
puts "faq.faq_category_id after save was faq.faq_category_id=[#{faq.faq_category_id}] [#{__FILE__}:#{__LINE__}]"
end
puts "faqs.class=[#{faqs.class}] [#{__FILE__}:#{__LINE__}]"
# unless children.empty?
# # children.map(&:id).each do |child_id|
# # FaqCategory.get(child_id).update(:parent_id => self.parent_id)
# # end
# children.map(&:id).each { |child_id| FaqCategory.get(child_id).update(:parent_id => self.parent_id) }
# end
end
end
class Faq
include DataMapper::Resource
property :id, Serial
property :faq_category_id, Integer, :required => true
property :question, String, :length => 250, :required => true
belongs_to :faq_category
# is :list, :scope => [:faq_category_id]
end
DataMapper::Logger.new("#{File.dirname(__FILE__)}/dm-faqs-problem.log", :debug)
DataMapper.logger.debug "Using DataMapper::VERSION=[#{DataMapper::VERSION}]"
DataMapper.setup(:default, 'sqlite3::memory:')
DataMapper.logger.debug "begin bootstraps #====================#\n"
DataMapper.auto_migrate!
@a = FaqCategory.create(:parent_id => 0, :name => "A")
@b = FaqCategory.create(:parent_id => 0, :name => "B")
@aa = FaqCategory.create(:parent_id => @a.id, :name => "Aa")
@ab = FaqCategory.create(:parent_id => @a.id, :name => "Ab")
## add some FAQs
Faq.create(:faq_category_id => @a.id, :question => "A1")
Faq.create(:faq_category_id => @b.id, :question => "B1")
DataMapper.logger.debug "end bootstraps #====================#\n"
## UPDATING THE :faq_category_id MANUALLY OUTSIDE OF A BEFORE BLOCK
faq = Faq.get(2)
puts faq.inspect # should show faq_category_id being 2
faq.faq_category_id = 0
faq.save
puts faq.inspect # should show faq_category_id being 0
## THE PROBLEM CODE COMES HERE>>>>
## UPDATING THE :faq_category_id INSIDE A BEFORE BLOCK IN THE PARENT MODEL
puts Faq.get(1).faq_category.inspect # should show the FaqCategory
puts Faq.get(1).faq_category_id # should show 1
f = FaqCategory.get(1)
puts f.inspect
# puts f.children.inspect
# puts f.children.first.inspect
# puts fc_child.inspect
f.destroy
puts Faq.get(1).faq_category_id # should show it as 0
puts Faq.get(1).inspect
%w(Aa Ab).each do |name|
puts FaqCategory.first(:name => name).inspect
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment