Skip to content


SQL syntax error with :has_many STI #119

pikelly opened this Issue · 0 comments

1 participant


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"

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

class Parameter < ActiveRecord::Base

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

if I now do "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
AND = parameters.reference_id (parameters.type = 'HostParameter' )

WHERE (( = '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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.