Forum: Ruby on Rails Building a model from a database view

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.
Larry W. (Guest)
on 2006-03-09 18:31
(Received via mailing list)
Hi,

I'm trying to use rails to build a model from a database view.

I get this output

C:\dev\v6\project>ruby script/generate scaffold Assignment
...
       error  Before updating scaffolding from new DB schema, try
creating a
tab
le for your model (Assignment)

Is this not possible in Rails?  It would be *really* helpful for my
application.
Matthias von Rohr (Guest)
on 2006-03-09 18:37
(Received via mailing list)
I think you need to have a table called 'assignments'

Matt
Lugovoi N. (Guest)
on 2006-03-09 18:40
(Received via mailing list)
there should be no problem, see this sample (just forget scaffolding for
views):

$ ruby script/console
Loading development environment.
>> class InfoSchemaTable < ActiveRecord::Base
>>   set_table_name 'information_schema.tables'
>> end
=> nil
>> InfoSchemaTable.find(:first, :conditions => "table_name = 'tables'")
=> #<InfoSchemaTable:0xf683aadc @attributes={"table_type"=>"VIEW",
"table_schema"=>"information_schema",
"user_defined_type_catalog"=>nil, "reference_generation"=>nil,
"self_referencing_column_name"=>nil, "table_name"=>"tables",
"table_catalog"=>"dl_v03_dev", "user_defined_name"=>nil,
"user_defined_type_schema"=>nil}>
>>

So, just manually create AR model for your view and explicitly
set_table_name of your view for it.
Larry W. (Guest)
on 2006-03-09 20:40
(Received via mailing list)
Actually, it was a typo that caused the original problem. Rails works
fine
for reading from views. The one catch I saw was that the view needs to
have
the id column included if you want to 'show' or 'edit' the original
items.
This topic is locked and can not be replied to.