I have a problem running find using composite primary keys. I have the following models: Code : - fold - unfold 1. class Stat < ActiveRecord::Base 2. set_primary_keys :user_id, :score_aspect 3. belongs_to :user Code : - fold - unfold 1. class User < ActiveRecord::Base 2. has_many :stats Stat.find runs fine when it can find a stat. But it returns error instead of "nil", when it cannot find a stat. Code : - fold - unfold 1. >> Stat.find(user, "valid_aspect") 2. => #<Stat id: 335580687, user_id: 1000, score_aspect: "valid_aspect" . . .> 3. 4. 5. >> Stat.find(user, "dfsdf") 6. ActiveRecord::RecordNotFound: Couldn''t find all Stats with IDs ([[#<User id: 1000, . . . "dfsdf"]])`stats`.`user_id`=1000 AND `stats`.`score_aspect`=''dfsdf'' 7. from /opt/local/lib/ruby/gems/1.8/gems/composite_primary_keys-2.2.2/lib/composite_primary_keys/base.rb:296:in `find_from_ids'' 8. from /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/base.rb:616:in `find'' 9. from (irb):41 I am using http://compositekeys.rubyforge.org/ . Please help. Thanks. Developing with Rails 2.3.2; Ruby 1.8.7 (2009-04-08 patchlevel 160) [powerpc-darwin9] -- Posted via http://www.ruby-forum.com/.
Quoting Learn by Doing <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>:> > I have a problem running find using composite primary keys. I have the > following models: > > > Code : - fold - unfold > > 1. class Stat < ActiveRecord::Base > 2. set_primary_keys :user_id, :score_aspect > 3. belongs_to :user > > > Code : - fold - unfold > > 1. class User < ActiveRecord::Base > 2. has_many :stats > > Stat.find runs fine when it can find a stat. But it returns error > instead of "nil", when it cannot find a stat. > >This is normal behavior, when calling find with a non-existant primary key, it throws an exception, ActiveRecord::RecordNotFound. Try it on one of your other classes, e.g., User.find(99999999). HTH, Jeffrey