[Rails] Re: Overriding or aliasing name of column in legacy database using Rails/ActiveRecord
Thanks for all the replies.
Let me ask you this:
If I do set up a view to rename the columns, can I override the create
and update method in ActiveRecord::Base to make it insert into the
original table (rather than the view)? Having looked at
ActiveRecord::Base a little bit it would seem that I can do this
without any ill effects.
Thanks,
Mike
On Dec 4, 1:28 pm, Jeff Lewis <jeff.bu...@gmail.com> wrote:
> Hi Mike,
>
> Having a db table column named object_id is going to be a problem for
> any ruby-based app, given object_id's significance as an object
> attribute in ruby (http://www.ruby-doc.org/core/classes/
> Object.html#M000339).
>
> If your rails app only needs to read from (and not write to) that
> legacy table, ... I'd create a view in the db which includes all the
> columns in that table but with that object_id column renamed to
> something like ob_id, and then read from that view instead of the
> table in your rails app.
>
> If your rails app needs to write to that table, and your rails app and
> the legacy app are the only two apps that use that legacy db, and you
> are able to modify that legacy app, .... I'd change that column name
> in the db to something like ob_id, then I'd search for ($ grep -Erni
> "object_id" /path/to/proj/root ) and replace all specific references
> to that specific column in the legacy code.
>
> Jeff
>
> On Dec 3, 12:22 pm, Mike B <mbern...@gmail.com> wrote:
>
> > I'm writing a Rails application against a legacy database. One of the tables
> > in this legacy database has a column named object_id. Unfortunately
> > object_id is also an attribute of every object in Ruby, so when ActiveRecord
> > is trying to use these objects to formulate a query it is using the Ruby
> > defined object_id, rather than the value that is in the database.
>
> > The legacy application is immense at well over a million lines of code, so
> > simply changing the name of the column in the database would be an option of
> > last resort.
>
> > Questions:
> > 1. Is there any way to make ActiveRecord/Rails use an alias or synonym for
> > this column? (i.e. such that in my app I can access the value using the
> > alias, but when AR generates SQL it uses the actual column name)
> > 2. Is there any way in Ruby to make the object_id method behave differently,
> > depending on who is calling it? (i.e. use database object_id when being
> > called by activerecord, otherwise return ruby object_id)
> > 3. Can I simply override the behavior of the object_id method in my model (I
> > assume this is a terrible idea, but had to ask)
>
> > Any suggestions are greatly appreciated.
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home