# Rails, Views, and Legacy Data

**URL:** <https://www.ruby-forum.com/t/rails-views-and-legacy-data/57731>\
**Category:** Rails\
**Created:** [April 13, 2006, 3:34pm UTC](https://www.ruby-forum.com/t/rails-views-and-legacy-data/57731 "2006-04-13T15:34:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![wpwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d6d6ee/32.png) [@wpwood](https://www.ruby-forum.com/u/wpwood)\
**Post date:** [April 13, 2006, 3:34pm UTC](https://www.ruby-forum.com/t/rails-views-and-legacy-data/57731/1 "2006-04-13T15:34:30Z")

</div>

I’d like to better understand the criticisms I’ve seen of Rails’ ability  
to handle legacy data. The ones I’m interested in are those based on  
Rails’ naming conventions for tables. In my experience in large  
corporate environments programmers / programs don’t access db tables  
directly. They access views of those tables. And, to my understanding  
(and I’m not a dba), the name of a view is independent of the underlying  
tables, and a column in a view does not need to be named the same as the  
column in the table the view maps to. So I’m wondering…

Can Rails be used on a view? Or does AR not support views for some  
reason?

Or is my understanding of the ability of views regarding (re)naming  
incorrect?

Thanks in advance for any help in understanding this.

Best regards,  
Bill

---

<div class="post-metadata">

**Author:** ![wpwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d6d6ee/32.png) [@wpwood](https://www.ruby-forum.com/u/wpwood)\
**Post date:** [April 13, 2006, 4:45pm UTC](https://www.ruby-forum.com/t/rails-views-and-legacy-data/57731/2 "2006-04-13T16:45:28Z")

</div>

Rails uses the MVC architecture (Model View Controller). The model is  
what connects to the database, so in your terminology, the model is the  
view. The model is responsible for communicating with the database and  
taking care of things such as relationships and validations.

If you were wondering 7 Rails can be integrated into some  
o:her system which uses these “views”, I would doubt this is possible  
although I really can’t say this accurately as I have no clue what  
system you are referring to.

Do not mistake Rails views for what you are referring to as a view.  
Views in rails are basically templates, what the designer might work  
with on a large project.

I’ve done a few projects in Rails with legacy schemas, I can’t say that  
does a particularly bad job of this, once the models are written you can  
forget entirely what kind of a mess the database might be in.

---

<div class="post-metadata">

**Author:** ![wpwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d6d6ee/32.png) [@wpwood](https://www.ruby-forum.com/u/wpwood)\
**Post date:** [April 13, 2006, 4:54pm UTC](https://www.ruby-forum.com/t/rails-views-and-legacy-data/57731/3 "2006-04-13T16:54:39Z")

</div>

On Thursday 13 April 2006 14:16, Bill W. wrote:

> I’d like to better understand the criticisms I’ve seen of Rails’ ability to  
> handle legacy data. The ones I’m interested in are those based on Rails’  
> naming conventions for tables. In my experience in large corporate  
> environments programmers / programs don’t access db tables directly. They  
> access views of those tables. And, to my understanding (and I’m not a  
> dba), the name of a view is independent of the underlying tables, and a  
> column in a view does not need to be named the same as the column in the  
> table the view maps to. So I’m wondering…  
> Can Rails be used on a view? Or does AR not support views for some reason?

A DBMS should present views to the world just like tables. There should  
be no  
difference at all in using them in Rails or anything else. Any database  
system that makes you treat views differently is Bad Software, although  
I’m  
not aware of any that do.

We use PostgreSQL for all new apps we write, and I now frequently make  
use of  
views to reshape third-party data into a more suitable format. You have  
to  
make rules to tell the view how to map inserts, deletes and updates to  
the  
base tables, but once that is done you can use them as tables. Other  
systems  
have different ways to make views updateable (IE, act like tables). I  
suppose a database that does not have updateable views could not be used  
like  
you describe, but I’ve not used any myself.

If you are switching an app to Rails, you could create a new,  
Rails-convention  
based schema, import your old data, and they produce a set of views so  
that  
legacy applications can continue to access data in the same way they  
always  
have (for the migration period). Obviously, if you want a legacy app  
and a  
Rails app accessing the same database, you must make sure you have all  
the  
necessary constraints in place so that one app or t’other doesn’t go  
inserting incomplete/incorrect data because the views are wrong. But  
that is  
always an issue with multiple applications hitting one DB.

Ashley

---

<div class="post-metadata">

**Author:** ![wpwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d6d6ee/32.png) [@wpwood](https://www.ruby-forum.com/u/wpwood)\
**Post date:** [April 13, 2006, 5:34pm UTC](https://www.ruby-forum.com/t/rails-views-and-legacy-data/57731/4 "2006-04-13T17:34:47Z")

</div>

On 4/13/06, Bill W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Can Rails be used on a view? Or does AR not support views for some reason?
> 
> Or is my understanding of the ability of views regarding (re)naming  
> incorrect?
> 
> Thanks in advance for any help in understanding this.

ActiveRecord is happy to work against a view. If you want to update  
your database as well as read it, you’ll need to make sure your DBMS  
supports that against views.

Don’t worry about table/view names, because it’s trivial to override  
the defaults.  
e.g.  
class Widget \< ActiveRecord::Base  
set\_table\_name ‘crazy\_table\_name’  
end

Several of the other ‘magic’ names can also be overridden:  
class WidgetWithLocking \< ActiveRecord::Base  
set\_locking\_column ‘locktastic’  
end

If you can arrange things so that your tables have a single integer  
primary key, you’ll be fine.  
If you can’t, you’ll have an uphill battle.

–Wilson.
