Variable column names

Hello.

I have a table that will have a changing number of columns. In order to
update them, I need to able to keep my column names in a variable, and
pass them to my model object. Such as:

@entry = Model.create
@columns.each do |column|
@entry.column = ‘something’
end

Obviously, this doesn’t work. Is there any method in ActiveRecord that
would allow me to pass a string as the column name? ie:

@entry.put_in_column(:name, :value)

Or is there a better way to handle a changing set of columns?

Thanks.

would allow me to pass a string as the column name? ie:

@entry.put_in_column(:name, :value)

Or is there a better way to handle a changing set of columns?

Maybe… update_attribute or update_attributes?

I would guess update_attributes is best as update_attribute would cause
a database query for each column. That way he would just build a hash of
column names and values…

-matthew

Hey,

are you saying that the table schema changes during the active
lifetime of your rails application? If not then I’ve misunderstood
you and the rest of this response is not relevant.

My first reaction would be to avoid messing with the underlying schema
at runtime… It’s not an informed reaction, it just strikes me as bad
news.

If schema changes happen so frequently that you can’t afford the
downtime of a deploy I’d suggest you were abusing the relational model

  • time to rethink your design.

If schema changes are not happening that frequently then things will
be a lot more stable if you redeploy with each schema change. Of
course, that’s just one guy’s opinion.

However, if changing the schema is what you’re set on doing then you
might have some success calling Model.reset_column_information as
you think is required - however, rails autogenerates methods based on
the schema and I don’t know enough about that end of things to be able
to say whether it’ll work at all - just a suggestion.

Regards,
Trevor

On 10/3/06, Bryan M [email protected] wrote:

end

Posted via http://www.ruby-forum.com/.

Trevor S.
http://somethinglearned.com

Thanks for the replies. I’ll see how well your suggestions work, but
Trevor does have a valid point. I am certainly abusing the design model.
My intention was to make the app as flexible and scalable as possible
(hence changing the schema). I’ve already had to redesign the model a
few times, and I’m trying really hard not to break the logic for the
parts that are already working smoothly. But you’re right, I probably
should redesign. DBs are just not an area of expertise for me, so its a
constant learning process.

Thanks again,
Bryan

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs