I know this questiong has been asked before but I cannot seem to find a satisfactory answer. I have a legacy database that i have to model and the primary key for one particular table is a varchar of email addresses. The problem i am having is when creating new records. I have tried things like the following but none seem to work: * setting the accessor to provide a default value if none passed * creating a before_create method to do the same thing * assigning a value to the pk after a call to user.new it would be greatly appreciated if someone could share their own experience in this area, thank you. -felix
Felix McCoey wrote:> I know this questiong has been asked before but I cannot seem to find a > satisfactory answer. I have a legacy database that i have to model and > the primary key for one particular table is a varchar of email > addresses. The problem i am having is when creating new records. I have > tried things like the following but none seem to work: > > * setting the accessor to provide a default value if none passed > * creating a before_create method to do the same thing > * assigning a value to the pk after a call to user.new > > it would be greatly appreciated if someone could share their own > experience in this area, thank you. > > -felixsorry for adding noise but this issue has really go me stuck. the wiki page for legacy db schemas does not seem to cover varchar pks http://wiki.rubyonrails.com/rails/pages/HowToUseLegacySchemas it does mention using ''before_validate_on_create'' but i am not sure how to use that method. thanks for your help -felix
Felix McCoey wrote:> Felix McCoey wrote: > >>I know this questiong has been asked before but I cannot seem to find a >>satisfactory answer. I have a legacy database that i have to model and >>the primary key for one particular table is a varchar of email >>addresses. The problem i am having is when creating new records. I have >>tried things like the following but none seem to work: >> >>* setting the accessor to provide a default value if none passed >>* creating a before_create method to do the same thing >>* assigning a value to the pk after a call to user.new >> >>it would be greatly appreciated if someone could share their own >>experience in this area, thank you. >> >>-felix > > sorry for adding noise but this issue has really go me stuck. the wiki > page for legacy db schemas does not seem to cover varchar pks > > http://wiki.rubyonrails.com/rails/pages/HowToUseLegacySchemas > > it does mention using ''before_validate_on_create'' but i am not sure how > to use that method.If you can modify the schema, the simplest thing I can think of is to add an auto_increment or serial id column for AR to use, and use validation (or constraints - not sure how that would work) to ensure that the email field is unique. Joins and foreign keys should work the same (I think - someone correct me if not). -- Alex