binarylogic / searchlogic

Searchlogic provides object based searching, common named scopes, and other useful tools.
http://rdoc.info/projects/binarylogic/searchlogic
MIT License
1.39k stars 133 forks source link

SQL syntax error with :has_many STI #119

Open pikelly opened 13 years ago

pikelly commented 13 years ago

I cannot understand what I have done wrong here. Have I defined the associations incorrectly?

create_table "parameters", :force => true do |t|
t.string   "name"
t.string   "value"
t.integer  "reference_id"
t.string   "type"
end

class Host < ActiveRecord::Base
has_many :host_parameters, :dependent => :destroy, :foreign_key => :reference_id
end

class Parameter < ActiveRecord::Base
end

class HostParameter < Parameter
belongs_to :host, :foreign_key => :reference_id
end

if I now do

Host.search( "host_parameters_name_eq"=>"hostmode", "host_parameters_value_eq"=>"development").all.count

then I get this SQL error

ActiveRecord::StatementInvalid: Mysql::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '= parameters.reference_id (parameters.type = 'HostParameter' ) WHERE ((param' at line 1: SELECT hosts.* FROM hosts INNER JOIN parameters ON hosts.id AND = parameters.reference_id (parameters.type = 'HostParameter' )
WHERE ((parameters.name = 'hostmode') AND (parameters.value = 'development'))

This is obviously incorrect SQL. The ON clause is all jumbled. If I search for either the value or the name then the search works it is just when both host_parameters_value and host_parameters_name are defined together that the problem occurs

Has anyone an idea as to why this is happening or how I might work around it?