This project is archived and is in readonly mode.

#5988 ✓stale
ronin-22245 (at lighthouseapp)

belongs_to and includes() & joins() bug

Reported by ronin-22245 (at lighthouseapp) | November 16th, 2010 @ 08:43 PM

Hi,

Calling SomeModel.includes(:users) doesn't generate INNER JOIN and throws TypeError:

class User < ActiveRecord::Base
  has_many :tunes
end

class Tune < ActiveRecord::Base
  belongs_to :user
end

class HomeController < ApplicationController
  def index
    @tunes = Tune.includes(:users).all
  end
end

Console output:

Started GET "/" for 127.0.0.1 at 2010-11-16 21:34:06 +0100
Processing by HomeController#index as HTML
Tune Load (0.6ms)  SELECT `tunes`.* FROM `tunes`
Completed   in 65ms

TypeError (can't convert nil into Integer):
app/controllers/home_controller.rb:3:in `index'

When trying to replace includes with join I'm getting this:

class HomeController < ApplicationController
  def index
    @tunes = Tune.joins(:user)
    @tunes.each do |t|
      t.user.name
    end
  end
end

Tune Load (0.8ms)  SELECT `tunes`.* FROM `tunes` INNER JOIN `users` ON `users`.`id` = `tunes`.`user_id`
User Load (0.9ms)  SELECT `users`.* FROM `users` WHERE (`users`.`id` = 1) LIMIT 1
CACHE (0.0ms)  SELECT `users`.* FROM `users` WHERE (`users`.`id` = 1) LIMIT 1
CACHE (0.0ms)  SELECT `users`.* FROM `users` WHERE (`users`.`id` = 1) LIMIT 1
CACHE (0.0ms)  SELECT `users`.* FROM `users` WHERE (`users`.`id` = 1) LIMIT 1
User Load (0.3ms)  SELECT `users`.* FROM `users` WHERE (`users`.`id` = 2) LIMIT 1
CACHE (0.0ms)  SELECT `users`.* FROM `users` WHERE (`users`.`id` = 1) LIMIT 1
CACHE (0.0ms)  SELECT `users`.* FROM `users` WHERE (`users`.`id` = 1) LIMIT 1
CACHE (0.0ms)  SELECT `users`.* FROM `users` WHERE (`users`.`id` = 1) LIMIT 1
CACHE (0.0ms)  SELECT `users`.* FROM `users` WHERE (`users`.`id` = 1) LIMIT 1
CACHE (0.0ms)  SELECT `users`.* FROM `users` WHERE (`users`.`id` = 1) LIMIT 1
CACHE (0.0ms)  SELECT `users`.* FROM `users` WHERE (`users`.`id` = 1) LIMIT 1
CACHE (0.0ms)  SELECT `users`.* FROM `users` WHERE (`users`.`id` = 1) LIMIT 1
CACHE (0.0ms)  SELECT `users`.* FROM `users` WHERE (`users`.`id` = 1) LIMIT 1
CACHE (0.0ms)  SELECT `users`.* FROM `users` WHERE (`users`.`id` = 1) LIMIT 1
CACHE (0.0ms)  SELECT `users`.* FROM `users` WHERE (`users`.`id` = 1) LIMIT 1

I'm using Rails 3.0.2 but the problem exists in 3.0.1 too.

Comments and changes to this ticket

  • ronin-22245 (at lighthouseapp)

    ronin-22245 (at lighthouseapp) November 16th, 2010 @ 08:47 PM

    This happens only when trying to call include on a model that belongs to another model. User.include(:tunes) works as expected.

  • Neeraj Singh

    Neeraj Singh November 24th, 2010 @ 05:10 AM

    • Importance changed from “” to “Low”

    I think you need to do

    @tunes = Tune.includes(:user)
    
  • ronin-22245 (at lighthouseapp)

    ronin-22245 (at lighthouseapp) November 24th, 2010 @ 06:29 AM

    I don't see the difference - it produces the same error.

  • rails

    rails February 25th, 2011 @ 12:00 AM

    • State changed from “new” to “open”

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

  • rails

    rails February 25th, 2011 @ 12:00 AM

    • State changed from “open” to “stale”

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