This project is archived and is in readonly mode.

#6735 new
anathematism

Rails 3 - Active Record Query that uses includes and select

Reported by anathematism | April 21st, 2011 @ 07:40 AM

I'm shifting my Rails 2 code to Rails 3. I've an active record query

Group.find(:all, :select => 'groups.name',:include => :owner)

In model groups.rb, i've an association defined:
belongs_to :owner, :class_name => "Member", :foreign_key =>"owner_id"

I'm getting an error while running above query:

ActiveModel::MissingAttributeError: missing attribute: owner_id

from /usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.0.6/lib/active_record/association_preload.rb:328:in `block in preload_belongs_to_association'
from /usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.0.6/lib/active_record/association_preload.rb:327:in `each'
from /usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.0.6/lib/active_record/association_preload.rb:327:in `preload_belongs_to_association'
from /usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.0.6/lib/active_record/association_preload.rb:121:in `block in preload_one_association'
from /usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.0.6/lib/active_record/association_preload.rb:115:in `each'
from /usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.0.6/lib/active_record/association_preload.rb:115:in `preload_one_association'
from /usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.0.6/lib/active_record/association_preload.rb:92:in `preload_associations'
from /usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.0.6/lib/active_record/relation.rb:68:in `block in to_a'
from /usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.0.6/lib/active_record/relation.rb:68:in `each'
from /usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.0.6/lib/active_record/relation.rb:68:in `to_a'
from /usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.0.6/lib/active_record/relation/finder_methods.rb:143:in `all'
from /usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.0.6/lib/active_record/relation/finder_methods.rb:105:in `find'
from /usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.0.6/lib/active_record/relation/finder_methods.rb:101:in `find'
from /usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.0.6/lib/active_record/base.rb:439:in `find'
from (irb):18
from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.6/lib/rails/commands/console.rb:44:in `start'
from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.6/lib/rails/commands/console.rb:8:in `start'

I'm using Rails 3.0.6. Seems like there's a bug introduced in the ActiveRecord 3.0.6. Kindly look into this issue and fix.

Thanks,

Atif

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Pages