say I have the following STI models
class Parent < ActiveRecord::Base
has_many :childs, foreign_key: 'parent_id' # forgive the english for sake of simplicity
#def childs
# Child.where(parent_id: id) # this works just fine BTW
#end
end
class BadParent < Parent
end
class GoodParent < Parent
end
and the following Child class
class Child
belongs_to :parent # parent_id lives on this
end
I dont care about setting the type on the Child so I don't care about creating a polymorphic association.
bad_parent = BadParent.create(name: 'Michael Jackson')
child = Child.create(name: 'Bobby', parent: bad_parent)
If I run
child.parent #=> <# BadParent > # AWESOME
bad_parent.childs #=> [] NO BUENO!!!
sql_statement = bad_parent.childs.to_sql #=> "SELECT `childs`.* FROM `childs` WHERE `childs`.`parent_id` = 1"
Child.find_by_sql(sql_statement) #=> [<# Child Object #>] BUENO!!!
Is there something I have to add to the association to make this work like find_by_sql
?