These are chat archives for rails-sqlserver/activerecord-sqlserver-adapter

23rd
Nov 2016
Ken Collins
@metaskills
Nov 23 2016 01:32
Hey John, assuming you have a base model for the connnection, and the associated model inherits form that ActiveRecord class/connection. Then the association from one model to the other is just normal Rails syntax
John Munyi
@wolfieorama
Nov 23 2016 11:33
@metaskills is the base model for the connection a must currently I am connecting directly like this
class Leave < ActiveRecord::Base establish_connection :development_sec self.table_name = 'dbo.leave' self.primary_key = 'ID' end
Ken Collins
@metaskills
Nov 23 2016 12:25
# app/models/legacy/base.rb
module Legacy
  class Base < ActiveRecord::Base
    establish_connection :development_sec 
    self.abstract_class = true
  end
end

# app/models/legacy/leave.rb
module Legacy
  class Leave < Legacy::Base
    self.table_name = 'dbo.leave' 
    self.primary_key = 'ID'
  end
end

# app/models/foo.rb
class Foo < ActiveRecord::Base 
  has_many :leaves, class_name: 'Legacy::Leave', foreign_key: '...'
end
So something like that...
John Munyi
@wolfieorama
Nov 23 2016 12:29
cool let me give a try
Ken Collins
@metaskills
Nov 23 2016 12:29
Assumes you have the right foreign key in that table.
And if you are using parent to save child associations, in this case Foo, then you have some pitfalls.
I wrote a blog post that kind of sums all this up... one sec...
There you go.
John Munyi
@wolfieorama
Nov 23 2016 12:33
Awesome .. will combine all the tricks there to see what I can get, in my case leave has a foreign key EmployeeID i suspect
John Munyi
@wolfieorama
Nov 23 2016 13:02
the issue now is i am getting NameError: uninitialized constant Leave when I try to access it via rails c
Ken Collins
@metaskills
Nov 23 2016 13:02
Did you set up the file/folder names right?
John Munyi
@wolfieorama
Nov 23 2016 13:02
yes
let me confirm
Ken Collins
@metaskills
Nov 23 2016 13:03
Rails needs the foler/file names to match the constants.
That's the magic of Ruby reloading in Rails, if those model names do not match. Rails has no idea where the constant is defined and can not require or reload for you.
I think you have a typo too.
It should be Legacy::Leave
Did you not include the class_name option for the association?
John Munyi
@wolfieorama
Nov 23 2016 13:08
I havent tested the association yet , was checking DB connection first
Ken Collins
@metaskills
Nov 23 2016 13:09
Ah... yea, if you setup those files.. .then there is no Leave the constant is namespaced and is instead Legacy::Leave
John Munyi
@wolfieorama
Nov 23 2016 13:10
Ah..yea the connection is ok now ... my assocition is with a Legacy model too
should it be the same way .. i guess it should
Ken Collins
@metaskills
Nov 23 2016 13:10
SWEET
John Munyi
@wolfieorama
Nov 23 2016 13:21
on the foreign key bit in Legacy::Employee class has_many :leaves, class_name: 'Legacy::Leave', foreign_key: (shouldnt this be EmployeeID though its doesnt seem to work
Ken Collins
@metaskills
Nov 23 2016 13:22
Do you have a foreign key in that leave table?
Not sure how your DB is modeled.
John Munyi
@wolfieorama
Nov 23 2016 13:22
will have to check its a remote DB if there is none is there a way round it
hmmm
Ken Collins
@metaskills
Nov 23 2016 13:23
Legacy::Leave.columns.map(&:name)
Will show the columns.
Assocations are sugar for things like convience @leaves ||= Legacy::Leave.where(...)
With more obviously.
But it really is up to you to make sure you are modeling the code inline with the DB.
John Munyi
@wolfieorama
Nov 23 2016 13:25
yes but cant really pick up which is the foreign key .. my assumption is EmployeeID
Ken Collins
@metaskills
Nov 23 2016 13:25
ActiveRecord associations allow so many options to make that happen. Like the foreign_key option.
John Munyi
@wolfieorama
Nov 23 2016 13:25
["ID", "LeaveTypeID", "StartDate", "LeaveDays", "EndDate", "EmployeeID", "StatusID", "Reason", "Supervisor", "LeaveAddress", "EmergencyAddress", "ContactName", "Telephone", "Remarks", "BackDate", "Replacementid", "IsExport", "ExportDate"]
Ken Collins
@metaskills
Nov 23 2016 13:25
Cool.
BTW, signing off for the day. I'll be back online later.
John Munyi
@wolfieorama
Nov 23 2016 13:26
cool, thanks for the help so far !!
appreaciated
John Munyi
@wolfieorama
Nov 23 2016 13:58
sadly the database was set up without foreign keys
Ken Collins
@metaskills
Nov 23 2016 16:43
Do you mean sans a DB FK constraint?
Or that leave.employeeid was empty?
Only the later is needed.
John Munyi
@wolfieorama
Nov 23 2016 16:52
I mean on the mssql leave table there is no foreign key
defined
Ken Collins
@metaskills
Nov 23 2016 16:55
If an employee has many leaves, then there should be an employee ID key in that table. You posted the output of Legacy::Leave.columns.map(&:name)and it included an EmployeeID field. So I'm totally confused when you say there is no foreign key.
John Munyi
@wolfieorama
Nov 23 2016 16:57
yes the EmployeeID is there but no set as a foreign key... thats what i mean ... not explicitly defined
Ken Collins
@metaskills
Nov 23 2016 16:57
module Legacy
  class Employee < Legacy::Base
    has_many :leaves, class_name: 'Legacy::Leave', foreign_key: 'EmployeeID'
  end
end
What does "explicitly defined" mean?
John Munyi
@wolfieorama
Nov 23 2016 16:58
i mean tagged as foreign key
when i do this has_many :leaves, class_name: 'Legacy::Leave', foreign_key: 'EmployeeID' i get an error when trying to query Employees
Ken Collins
@metaskills
Nov 23 2016 16:59
ActiveRecord could care less if that field had a technical DK constraint setup on that field or not. You are telling it with that code above.
So if you do employee = Legacy::Employee.find(42) and there are rows in the leaves table with EmployeeID equal to 42
Then it works.
That's just raw DB stuff. MSSQL, PG, MySQL, etc.
John Munyi
@wolfieorama
Nov 23 2016 17:01
now that where the issue is with that line in the employee.rb then in rails console this doesnt work employee = Legacy::Employee.find(42)
Ken Collins
@metaskills
Nov 23 2016 17:01
Error please.
John Munyi
@wolfieorama
Nov 23 2016 17:03
NameError: uninitialized constant Legacy::Employee::EmployeeID
aaah men my brains are off today
Ken Collins
@metaskills
Nov 23 2016 17:03
I think you have a ruby typo
John Munyi
@wolfieorama
Nov 23 2016 17:03
i was missin
Ken Collins
@metaskills
Nov 23 2016 17:04
Quotes :)
John Munyi
@wolfieorama
Nov 23 2016 17:04
yes
aaaah men sorry could see it !
now it works
Ken Collins
@metaskills
Nov 23 2016 17:08
AWESOME :tada: