From: Brian Candler Date: 2009-03-12T17:50:44+09:00 Subject: Re: help with refactor and db/ar advice ball wrote: > 1. Is there a way to take advantage of the schema so that I don't have > to re-write it in the load function? puts FoodDescription.columns.inspect puts FoodDescription.map { |c| c.name }.inspect > 2. Is there a way I can write a generic load function (taking > advantage of #1 if available, or not if not). As long as the columns in the text source appear in the same order as the columns in the database definition you should be fine. I think this ordering is guaranteed by the database, otherwise you couldn't meaningfully do "INSERT INTO foo VALUES (x,y,z)" (without naming the columns) or "SELECT * FROM foo" > 3. What is the "better" way (than ][1..-2]) to strip a string of a > starting/ending ~ Do it all in one go. A couple of options: records = line.split(/^/).map { |rec| rec[1..-2] } records = line.scan(/~([^~^]*)~/) but they won't work unless *all* your fields are ~ delimited. So this might work better for you: records = line.split(/^/).map { |rec| rec.sub(/~(.*)~) { $1 } } > 4. Should I just use an AR generated primary key, rather than the data > sets ASCII primary key? (many of the tables in the datasets have > composite keys, in which I will not implement, so I am curious if I > just create all of the primary keys through AR insted). ActiveRecord has some hard-coded assumptions about primary keys. If you override its assumption that the key is called "id" then it won't generate keys automatically for new records. If you have non-integer primary key then it may or may not work. It certainly doesn't handle composite primary keys; I saw a plugin which claimed to do this some time ago, but I don't know if it's been kept up to date. So my advice with AR is: if you are working with an existing database/schema that you don't control then try using non-standard primary keys, but if you control the schema yourself, follow AR's assumption of an integer primary key. -- Posted via http://www.ruby-forum.com/.