activerecord - Rails - How to specify foreign key explicitly when using :include method in controller -


i've got rails controller i'm trying return json object composed of several associated models derived legacy sql server database. i'm new , experimenting, i'm using ":include" on 1 associated model until can work, , ante.

controller:

class slotmachineentitycontroller < applicationcontroller   def retrieveall       @blah = machine.where("machine.casinoid = 2").find(1, :select => "machine.machineid", :include => :slot_instance)        respond_to |format|           format.json { render :json => @blah }       end   end end 

the error is:

tinytds::error: invalid column name 'machine_id'.: ... [master].[slotinstance].[active] t1_r10 [master].[machine] left outer join [master].[slotinstance] on [master].[slotinstance].[machine_id] = [master].[machine].[machineid] [master].[machine].[machineid] = @0 , (machine.casinoid = 2)', n'@0 int', @0 = 1

presumably error takes place when tries execute ":include => :slot_instance" , reverts default naming convention looking "machine_id" instead of "machineid" key used in database. cannot find syntax wherein specify :include method foreign key machineid , not machine_id.

for reference, relevant models are:

class machine < activerecord::base     self.table_name = "master.machine"     set_primary_key "machineid"      has_one :slot_instance     belongs_to :manufacturer, :foreign_key => "manufacturerid"     belongs_to :casino, :foreign_key => "casinoid"     belongs_to :customer, :foreign_key => "customerid" end  class slotinstance < activerecord::base     self.table_name = "master.slotinstance"     set_primary_key "slotinstanceid"      belongs_to :game_configuration, :foreign_key => "gameconfigurationid"     belongs_to :customer, :foreign_key => "customerid"     belongs_to :casino, :foreign_key => "casinoid"     belongs_to :location, :foreign_key => "locationid"     belongs_to :machine, :foreign_key => "machineid" end 

thanks help!

you need specify foreign key in has_one relation in machine class, i.e.

has_one :slot_instance, :foreign_key => "machineid" 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -