Hi,

I'm updating a small site to include ecommerce functionality for the
single product they sell, and this is my first experience with Ruby
(I'm more familiar with PHP, though I've come to much prefer Ruby).
The site was built with Sinatra and DataMapper (DM), and I'm trying to
figure out the best way to use DataMapper with ActiveMerchant (which
really seems like the most mature Ruby payment gateway solution).
Unfortunately, all the tutorials/literature about setting up
ActiveMerchant assume the developer is using Rails and ActiveRecord
(AR), and there are a few questions I haven't been able to find clear
answers to in either the DataMapper documentation or via Google.

E.g., I'm using a PeepCode tutorial which does a nice job of
explaining ActiveMerchant in a Rails context by creating a stripped
down sample app.  Here's a relevant code portion from the tutorial,
I've indicated the points I need help on with <-[#] indicators.

# ActiveRecord code
class Order < ActiveRecord::Base
  has_many :order_transactions,
                   :dependent => :destroy <-[1]

  # some code...

  def authorize_payment(credit_card, options = {})
        # some code...
    transaction do  <-[2]
      authorization = OrderTransaction.authorize(amount, credit_card,
options)
      order_transactions.push(authorization) <-[3]

          # some code...
    end
  end
end

1. I understand that I need to require 'dm-constraints' in order to
realize foreign key constraint functionality in DM, but I get an error
when I call DataMapper.auto_migrate! in my app file (below):

/Library/Ruby/Gems/1.8/gems/dm-core-0.10.1/lib/dm-core/adapters/
data_objects_adapter.rb:162:in `execute_non_query': Can't create table
'./store_db/#sql-8d7_119.frm' (errno: 150) (DataObjects::SQLError)
        from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.1/lib/dm-core/adapters/
data_objects_adapter.rb:162:in `execute'
        from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.1/lib/dm-core/adapters/
data_objects_adapter.rb:266:in `with_connection'
        from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.1/lib/dm-core/adapters/
data_objects_adapter.rb:160:in `execute'
        from /Library/Ruby/Gems/1.8/gems/dm-constraints-0.10.1/lib/dm-
constraints/migrations.rb:86:in `create_relationship_constraint'
        from /Library/Ruby/Gems/1.8/gems/dm-constraints-0.10.1/lib/dm-
constraints/migrations.rb:268:in `send'
        from /Library/Ruby/Gems/1.8/gems/dm-constraints-0.10.1/lib/dm-
constraints/migrations.rb:268:in `execute_each_relationship'
        from /Library/Ruby/Gems/1.8/gems/dm-constraints-0.10.1/lib/dm-
constraints/migrations.rb:267:in `each_value'
        from /Library/Ruby/Gems/1.8/gems/dm-constraints-0.10.1/lib/dm-
constraints/migrations.rb:267:in `execute_each_relationship'
        from /Library/Ruby/Gems/1.8/gems/dm-constraints-0.10.1/lib/dm-
constraints/migrations.rb:258:in `auto_migrate_up_with_constraints!'
        from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.1/lib/dm-core/
migrations.rb:49:in `send'
        from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.1/lib/dm-core/
migrations.rb:49:in `repository_execute'
        from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.1/lib/dm-core/model/
descendant_set.rb:33:in `each'
        from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.1/lib/dm-core/model/
descendant_set.rb:33:in `each'
        from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.1/lib/dm-core/
migrations.rb:48:in `repository_execute'
        from /Library/Ruby/Gems/1.8/gems/dm-constraints-0.10.1/lib/dm-
constraints/migrations.rb:23:in `auto_migrate_up!'
        from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.1/lib/dm-core/
migrations.rb:22:in `auto_migrate!'
        from store.rb:36

Is there some conflict with auto_migrate! and dm-constraints that I
need to work around?

2. How do I use transactions in DM?  In AR, it appears to simply be:

transaction do
  #some code...
end

In the DM api, I see there's a DataMapper::Transaction class.  Would I
do something like the following:

transaction = DataMapper::Transaction.new(DataMapper.repository
(:default))
transaction.begin
transaction.commit do
  #some code...
end

Is this even close to right?

3. In AR, the .push method appears to create new through associations,
e.g.:

class Order < ActiveRecord::Base
  has_many :order_transactions

  # some code...
  order_transactions.push(some_object)
end

What would be the DM equivalent, e.g. for:

class Order
  include DataMapper::Resource
  # some properties...
  has n, :order_transactions

  ???
end


Apologies in advance if this stuff seems basic, I'm just trying to get
more familiar with using DataMapper and Ruby.  Any help is sincerely
appreciated!

Cheers,
Jeff

p.s., hope the code formatting doesn't end up too bad.

--

You received this message because you are subscribed to the Google Groups 
"DataMapper" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/datamapper?hl=en.


Reply via email to