This project is archived and is in readonly mode.
joins() on has_one through generates SQL query with "IS NULL"
Reported by avioing | November 29th, 2010 @ 09:14 PM
using ruby 1.9.2, rails 3.0.3
Post
belongs_to :user_project
has_one :project, :through => :user_project
UserProject
belongs_to :project
has_many :posts
Project
has_many :user_projects
has_many :posts, :through => :user_projects
here is the problem:
irb> Post.joins(:project)
=> []
here is the generated query:
SELECT posts
.* FROM posts
INNER JOIN
user_projects
ON posts
.id
IS
NULL INNER JOIN projects
ON
projects
.id
=
user_projects
.project_id
where is "IS NULL" coming from?
Comments and changes to this ticket
-
Joeri Samson December 15th, 2010 @ 05:07 PM
I have the same problem (also using Rails 3.0.3, but using JRuby), additionally if I make the join 1 level deeper I get an INNER JOIN
table_name
ON 0.class FociSample < ActiveRecord::Base belongs_to :foci_group has_one :source_well, :through => :foci_group end class FociGroup < ActiveRecord::Base belongs_to :source_well, :foreign_key => "source_id", :class_name => "CultureWell" end class CultureWell < ActiveRecord::Base belongs_to :culture_plate end
When trying
FociSample.joins(:source_well => :culture_plate)
SELECT
foci_samples.* FROMfoci_samples
INNER JOINfoci_groups
ONfoci_samples
.id
IS NULL INNER JOINculture_wells
ON 0 INNER JOINculture_plates
ONculture_plates
.id
=culture_wells
.culture_plate_id
Adding an additional level
class CulturePlate < ActiveRecord::Base belongs_to :culture_batch end
FociSample.joins(:source_well => {:culture_plate => :culture_batch})
SELECT
foci_samples.* FROMfoci_samples
INNER JOINfoci_groups
ONfoci_samples
.id
IS NULL INNER JOINculture_wells
ON 0 INNER JOINculture_plates
ONculture_plates
.id
=culture_wells
.culture_plate_id
INNER JOINculture_batches
ONculture_batches
.id
=culture_plates
.culture_batch_id
-
Jon Leighton December 21st, 2010 @ 08:16 PM
- State changed from new to duplicate
- Importance changed from to Low
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>