From: Leslie Viljoen Date: 2006-11-22T07:42:53+09:00 Subject: Re: Active Record out of rails: still assumes id? On 11/21/06, Gabriele Marrone wrote: > > On 21/nov/06, at 14:12, Chris Gallagher wrote: > > > This is clearly due to the fact that I dont have any id fields in my > > database as i thought that active record would do something similar to > > rails and that i wouldnt have to define them? Obviously this isnt > > quite > > the case? > > I guess your table will have a key, anyway. You just have to tell > ActiveRecord what is the key column, as in: > > class Person < ActiveRecord::Base > set_primary_key :full_name # overrides id with full_name > end > > Some times you will need a composite primary key, but ActiveRecord > doesn't support them by default. If you need them, however, you can > install the composite_primary_keys gem (http:// > compositekeys.rubyforge.org/) and then do something like this: > > class Person < ActiveRecord::Base > set_primary_keys :first_name, :last_name > end Though if you do use set_primary_key, remember that in the model object, the field will still be called 'id' even though it's mapped to another column in the table. You also then assume responsibility for incrementing and setting that ID before every save, since the ID can be in any format. I have: class ArchiveLog < MsSqlTable set_table_name "ArchiveLog" set_primary_key "ArchiveLogID" end ... next_id = ArchiveLog.maximum(:ArchiveLogID) if next_id.nil? next_id = 0 else next_id += 1 end archiveLog = ArchiveLog.new archiveLog.id = next_id archiveLog.ArchiveDate = @startTime archiveLog.CurrentDataStartDate = @archiveBorderTime ..etc. Les -- Man's unfailing capacity to believe what he prefers to be true rather than what the evidence shows to be likely and possible has always astounded me. We long for a caring Universe which will save us from our childish mistakes, and in the face of mountains of evidence to the contrary we will pin all our hopes on the slimmest of doubts. God has not been proven not to exist, therefore he must exist. - Prokhor Zakharov