Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Composite primary key fails on relationship with empty result set #59

Closed
maxstudener opened this issue Mar 29, 2012 · 9 comments
Closed

Comments

@maxstudener
Copy link

RuntimeError: Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id

Error is present on Rails 3.2.1
Using jruby 1.6.7
JDBC adpater https://github.com/jruby/activerecord-jdbc-adapter.git
Composite primary key

@flyerhzm
Copy link
Owner

could you print the error message?

@shmeltex
Copy link

Here is the simple example:

class Item < ActiveRecord::Base
  belongs_to :category
  delegate :name, :to => :category, :prefix => true, :allow_nil => true
end
class Category < ActiveRecord::Base
  has_many :items
end

And here is the error from console:

    >> Item.new.category_name
    RuntimeError: Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
    from /Users/xxxx/.rvm/gems/ruby-1.9.3-p125/gems/bullet-2.3.0/lib/bullet.rb:6:in `ar_key'
    from /Users/xxxx/.rvm/gems/ruby-1.9.3-p125/gems/bullet-2.3.0/lib/bullet/detector/association.rb:36:in `add_possible_objects'
    from /Users/xxxx/.rvm/gems/ruby-1.9.3-p125/gems/bullet-2.3.0/lib/bullet/active_record31.rb:78:in `reader'
    from /Users/xxxx/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.2/lib/active_record/associations/builder/association.rb:44:in `block in define_readers'
    from /Users/xxxx/web/Bullet/app/models/item.rb:2:in `category_name'
    from (irb):3
    from /Users/xxxx/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.2/lib/rails/commands/console.rb:47:in `start'
    from /Users/xxxx/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.2/lib/rails/commands/console.rb:8:in `start'
    from /Users/xxxx/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.2/lib/rails/commands.rb:41:in `<top (required)>'
    from /Users/xxxx/web/Bullet/script/rails:6:in `require'
    from /Users/xxxx/web/Bullet/script/rails:6:in `<top (required)>'
    from -e:1:in `load'
    from -e:1:in `<main>'

@flyerhzm
Copy link
Owner

I have fixed the issue in HEAD code, please try it and let me if it works for you.

@shmeltex
Copy link

Still fails for me:

    >> Item.new.category_name
    RuntimeError: Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
    from /Users/xxxx/.rvm/gems/ruby-1.9.3-p125/bundler/gems/bullet-92a75b9fc1b9/lib/bullet/detector/association.rb:34:in `add_possible_objects'
    from /Users/xxxx/.rvm/gems/ruby-1.9.3-p125/bundler/gems/bullet-92a75b9fc1b9/lib/bullet/active_record31.rb:78:in `reader'
    from /Users/xxxx/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.2/lib/active_record/associations/builder/association.rb:44:in `block in define_readers'
    from /Users/xxxx/web/Bullet/app/models/item.rb:2:in `category_name'
    from (irb):1
    from /Users/xxxx/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.2/lib/rails/commands/console.rb:47:in `start'
    from /Users/xxxx/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.2/lib/rails/commands/console.rb:8:in `start'
    from /Users/xxxx/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.2/lib/rails/commands.rb:41:in `<top (required)>'
    from /Users/xxxx/web/Bullet/script/rails:6:in `require'
    from /Users/xxxx/web/Bullet/script/rails:6:in `<top (required)>'
    from -e:1:in `load'
    from -e:1:in `<main>'

@flyerhzm
Copy link
Owner

please pull codes and try again

@shmeltex
Copy link

Works now. Thank you!

@tute
Copy link

tute commented Mar 30, 2012

Thanks for the fix, using master for now.

@ghost
Copy link

ghost commented Apr 2, 2012

I had the same issue and the master branch did fix it. Thanks for the fix, it works nice.

Just out of interest that Bullet can break the ActiveRecord query? Is it a wrong approach or has to be done in this way? Cos for me intercepting the Rails code is not quiet good...

@flyerhzm
Copy link
Owner

flyerhzm commented Apr 2, 2012

@lecky-ambit bullet does not change ActiveReord query, it just parses the results of ar queries.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants