Skip to content
This repository has been archived by the owner on Mar 30, 2022. It is now read-only.

Wrong join condition in SQL #102

Open
harmaty opened this issue Jul 4, 2012 · 1 comment
Open

Wrong join condition in SQL #102

harmaty opened this issue Jul 4, 2012 · 1 comment

Comments

@harmaty
Copy link

harmaty commented Jul 4, 2012

I have 3 models

class Login  < ActiveRecord::Base
  has_many :roles
end

class Property < ActiveRecord::Base
  has_many :roles
  has_one :owner, :through => :roles, :source => :login, :conditions => {:roles =>  {:relationship_type => 'owner'}}
end

class Role < ActiveRecord::Base
  belongs_to :login
  belongs_to :property
end
Property.search( :owner_email_eq => '[email protected]')

Generates following SQL:

 SELECT `properties`.* FROM `properties` 
   LEFT OUTER JOIN `roles` ON `properties`.`id` = `roles`.`property_id` 
   LEFT OUTER JOIN `logins` ON `roles`.`relationship_type` = 'owner' 
   WHERE `logins`.`email` = '[email protected]'

Condition: roles.relationship_type = 'owner' is wrong, it should be logins.id = roles.login_id

Problem appears only when condition for has_one :owner relation is defined, without condition search works fine

@plukacs
Copy link

plukacs commented Oct 24, 2012

I have the same problem.

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

No branches or pull requests

2 participants