Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

SQL syntax error with :has_many STI #119

Open
pikelly opened this Issue · 0 comments

1 participant

@pikelly

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?

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.