jwood / tenacity

A database client independent way of managing relationships between models backed by different databases.
MIT License
118 stars 17 forks source link

Support for non-default primary key #43

Closed gaustin closed 2 years ago

gaustin commented 11 years ago

I have models like:

class Dealer < ActiveRecord::Base
  self.primary_key = "dealer_id"
end

class Quote
  include Mongoid::Document, Tenacity
  t_belongs_to :dealer
end

When I try to create a quote I get:

NoMethodError: undefined method `find_by_id' for Dealer(64 columns):Class
  from /Users/gaustin/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.7/lib/active_record/dynamic_matchers.rb:27:in `method_missing'
  from /Users/gaustin/.rvm/gems/ruby-1.9.3-p194/gems/tenacity-0.6.0/lib/tenacity/orm_ext/activerecord.rb:60:in `_t_find'
  from /Users/gaustin/.rvm/gems/ruby-1.9.3-p194/gems/tenacity-0.6.0/lib/tenacity/instance_methods.rb:23:in `block in _t_verify_associates_exist'
  from /Users/gaustin/.rvm/gems/ruby-1.9.3-p194/gems/tenacity-0.6.0/lib/tenacity/instance_methods.rb:19:in `each'
  from /Users/gaustin/.rvm/gems/ruby-1.9.3-p194/gems/tenacity-0.6.0/lib/tenacity/instance_methods.rb:19:in `_t_verify_associates_exist'
  from /Users/gaustin/.rvm/gems/ruby-1.9.3-p194/gems/tenacity-0.6.0/lib/tenacity/orm_ext/mongoid.rb:92:in `block in _t_initialize_tenacity'
  from /Users/gaustin/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.7/lib/active_support/callbacks.rb:407:in `_run__4337379813175508762__save__2111390752293531923__callbacks'
  from /Users/gaustin/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.7/lib/active_support/callbacks.rb:405:in `__run_callback'
  from /Users/gaustin/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.7/lib/active_support/callbacks.rb:385:in `_run_save_callbacks'
  from /Users/gaustin/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.7/lib/active_support/callbacks.rb:81:in `run_callbacks'
  from /Users/gaustin/.rvm/gems/ruby-1.9.3-p194/gems/mongoid-3.0.9/lib/mongoid/callbacks.rb:114:in `run_callbacks'
  from /Users/gaustin/.rvm/gems/ruby-1.9.3-p194/gems/mongoid-3.0.9/lib/mongoid/persistence/insertion.rb:23:in `prepare'
  from /Users/gaustin/.rvm/gems/ruby-1.9.3-p194/gems/mongoid-3.0.9/lib/mongoid/persistence/operations/insert.rb:26:in `persist'
  from /Users/gaustin/.rvm/gems/ruby-1.9.3-p194/gems/mongoid-3.0.9/lib/mongoid/persistence.rb:50:in `insert'
  from /Users/gaustin/.rvm/gems/ruby-1.9.3-p194/gems/mongoid-3.0.9/lib/mongoid/persistence.rb:79:in `save'
  from /Users/gaustin/.rvm/gems/ruby-1.9.3-p194/gems/mongoid-3.0.9/lib/mongoid/persistence.rb:231:in `block in create'
  from /Users/gaustin/.rvm/gems/ruby-1.9.3-p194/gems/mongoid-3.0.9/lib/mongoid/threaded/lifecycle.rb:173:in `_creating'
  from /Users/gaustin/.rvm/gems/ruby-1.9.3-p194/gems/mongoid-3.0.9/lib/mongoid/persistence.rb:229:in `create'
  from (irb):1
  from /Users/gaustin/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.7/lib/rails/commands/console.rb:47:in `start'
  from /Users/gaustin/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.7/lib/rails/commands/console.rb:8:in `start'
  from /Users/gaustin/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.7/lib/rails/commands.rb:41:in `<top (required)>'
  from script/rails:6:in `require'
  from script/rails:6:in `<main>'

I tried specifying the primary key by passing ":primary_key => 'dealer_id'" to the t_belongs_to association.

Suggestions?

jwood commented 11 years ago

Sorry, Tenacity doesn't support this.

My work on Tenacity is currently limited to bug fixes. I have no plans to add any features to the library in the near future. However, if you're interested in adding this feature, I'd be more than happy to accept a pull request.

gaustin commented 11 years ago

Thanks for the quick response.

I'm currently supporting this functionality with my own little library, but it's way too MongoDB-specific. I'd really like all of the cross-DBMS support in the same library.

I'll try to workup a pull request in the next few weeks.

jwood commented 11 years ago

Cool. Don't hesitate to ping me if you have any questions.

masterweily commented 10 years ago

@jwood Hi, i have monkey-patched this functionality for has_many relation, and thinking about making a pull request with full support for non-default primary_key. Would that be cool?

jwood commented 10 years ago

@masterweily yep, that would be great. If you submit a PR, and all of the tests pass, I will happily merge it in and cut a new version.