Building a model from a database view


#1

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.


#2

I think you need to have a table called ‘assignments’

Matt


#3

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.


#4

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.