Forum: Ruby on Rails Rails, Views, and Legacy Data

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Bill W. (Guest)
on 2006-04-13 17:34
(Received via mailing list)
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
Christian (Guest)
on 2006-04-13 18:45
(Received via mailing list)
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.
Ashley M. (Guest)
on 2006-04-13 18:54
(Received via mailing list)
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
Wilson B. (Guest)
on 2006-04-13 19:34
(Received via mailing list)
On 4/13/06, Bill W. <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.
This topic is locked and can not be replied to.