Ruby / Ruby on Rails:
I am building a database wrapper in a class. I want to expose a string array of people (ex: ['john','jane','lucie']) which basically wraps a comma-delimited string column in a legacy database (names are in the db as: 'john,jane,lucie').
I'd like to be able to do: foo.people[1] = 'mark', foo.people.pop or even foo.people.push('richard') and have the changes reflected in the database.
How do I hook into an array to be able to perform the underlying database operations? I'm not quite sure how to implement this correctly.
People succeed in answering Carl Mercier's questions 23% of the time (5 successes in 22 attempts).
Answers by: Rich Collins
I would just use ActiveRecord's callback system.
before_save :set_legacy_column_from_array
def after_find
set_array_from_legacy_column
end
forgot to format correctly, just read it from your email
Totally makes sense! Should have thought of that before... that's why I came here to ask the Ruby guru (aka: Rich)!
Thanks :) It's a lot easier than I thought after all!