Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

belongs_to and includes() & joins() bug

#5988

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.

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

State: stale
Milestone: none
Assigned to: nobody
Importance: Low

Activity

  1. ronin-22245 (at lighthouseapp)
    ronin-22245 (at lighthouseapp)

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

    November 16th, 2010 @ 08:47 PM

  2. Neeraj Singh
    Neeraj Singh
    • Importance changed from to Low

    I think you need to do

    @tunes = Tune.includes(:user)
    

    November 24th, 2010 @ 05:10 AM

  3. ronin-22245 (at lighthouseapp)
    ronin-22245 (at lighthouseapp)

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

    November 24th, 2010 @ 06:29 AM

  4. rails
    rails
    • 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.

    February 25th, 2011 @ 12:00 AM

  5. rails
    rails
    • State changed from open to stale

    February 25th, 2011 @ 12:00 AM