Generate model from database

hello i want to ask, how to generate model from table in my database.
That’s possibel ?

On 18 October 2011 15:01, prasetya utama [email protected] wrote:

hello i want to ask, how to generate model from table in my database.
That’s possibel ?

If you have a table widgets then all you need for the model is a file
models/widget.rb containing

class Widget < ActiveRecord::Base
end

Rails will do the rest.

Colin

@colin : i haven’t understand, can you explain how to make it. may be
script for generate it like if i want generate controller (ruby
script/generate …)

On 18 October 2011 15:59, prasetya utama [email protected] wrote:

Please don’t top post, it makes it difficult to follow the thread,
insert your reply at appropriate points in previous message. Thanks.

@colin : i haven’t understand, can you explain how to make it. may be
script for generate it like if i want generate controller (ruby
script/generate …)

Can you explain exactly what it is you are trying to generate. You
said that you wanted the model, is it actually the complete scaffold
that you want?

I think you can use the scaffold generator and pass it the option
–skip-migration to stop it making a migration to create the database,
but you still have to pass the fields you are interested in. I have
never done this however.

http://guides.rubyonrails.org/getting_started.html#getting-up-and-running-quickly-with-scaffolding
explains how to use the scaffold generator.

Colin

yes i want to generate model, but the table has been created in
databases. And i want generate that table :slight_smile:

On 18 October 2011 16:25, prasetya utama [email protected] wrote:

yes i want to generate model, but the table has been created in
databases. And i want generate that table :slight_smile:

I asked last time that you don’t top post, it makes it difficult to
follow the thread,
insert your reply at appropriate points in previous message. This
time you have not even quoted the previous message so anyone reading
this will have not idea what you are talking about without looking
back through the thread.

You say that you want to generate just the model, so you do not want
to generate the full scaffold. As I said previously there is no need
to “generate” the model, just create a file models/your_model.rb with
the class definition in as I showed in my first reply. Rails will
automatically pick up the fields from the database at run time.

I think you might benefit from reading through the Rails Guides and
from working through some tutorials. railstutorial.org is good and
free to use online. Make sure that you have the version of rails to
match the tutorial you are using.

Colin

Oct 18, 11:40am, Colin L. [email protected] wrote:

This is a problem I am also facing - trying to set up a rails app with
a legacy database. The minimal model file Colin suggests works fine
for access to the database, but I would love it if the scaffold (view,
controller, …) files could be generated as if all the table fields
were explicitly used in the generate scaffold command. I know I would
still need to modify them, but I would rather not have to add all the
fields for all the tables into the various view files. I have found
Dr. Nic’s magic_model_generator gem, and if I can get it to do what I
need with rails 2, which I am still not clear about, then I’ll have to
see if I can modify it to work with rails 3. Can anybody suggest
anything else that will generate a more complete (as in using the
fields from the database) set of view and other files?

On Tue, Oct 18, 2011 at 5:05 PM, genterminl
[email protected] wrote:

anything else that will generate a more complete (as in using the
fields from the database) set of view and other files?

Can you just get a list of the columns in that table, then generate
the scaffold normally, using those fields as the parameters to the
call, then delete the migration file?

Colin L. wrote in post #1027233:

On 18 October 2011 16:25, prasetya utama [email protected] wrote:

yes i want to generate model, but the table has been created in
databases. And i want generate that table :slight_smile:

I asked last time that you don’t top post, it makes it difficult to
follow the thread,
insert your reply at appropriate points in previous message. This
time you have not even quoted the previous message so anyone reading
this will have not idea what you are talking about without looking
back through the thread.

You say that you want to generate just the model, so you do not want
to generate the full scaffold. As I said previously there is no need
to “generate” the model, just create a file models/your_model.rb with
the class definition in as I showed in my first reply. Rails will
automatically pick up the fields from the database at run time.

I think you might benefit from reading through the Rails Guides and
from working through some tutorials. railstutorial.org is good and
free to use online. Make sure that you have the version of rails to
match the tutorial you are using.

Colin

Thanks colin, for your advice :slight_smile: and your coment

I believe you can also pass a “–skip-migration” option to scaffold so
it
won’t generate the migration.

The whole point is I have lots of tables with lots of columns, and I
don’t
want to have to do all that typing. The magic_model_generator creates
all
the model files, and it can obviously see all the columns in the
database,
I’m just surprised that nobody has extended it or created something else
to
automatically fill in the view files. (And I do use the
–skip-migration
option as suggested in the other response.)

On Oct 19, 4:16pm, genterminl [email protected]
wrote:

The whole point is I have lots of tables with lots of columns, and I don’t
want to have to do all that typing. The magic_model_generator creates all
the model files, and it can obviously see all the columns in the database,
I’m just surprised that nobody has extended it or created something else to
automatically fill in the view files. (And I do use the --skip-migration
option as suggested in the other response.)

I looked for this same answer a while back and determined it was silly
to do such - mainly because the only way around it IS to build
everything by hand.

rails g scaffold TableName --skip-migration field:string field2:string
field3:string field4:boolean

That’s going to be the only way around it. And you may have to
explicitly define the table name if it doesn’t match rails naming
structures. Any way you spin it, you’re going to have to redesign
forms for the data anyway.

Check into the formtastic gem - it might aid you in building forms
from scratch. I believe there’s a Railscast on it as well.

Well you don’t exactly have to do it by hand. It’s pretty easy to
write a
script to generate the command for you. Just change table_name to
whatever
your model is…

table_name = “Post”
output = [ “rails g scaffold #{table_name} --skip-migration” ]
ignore_columns = [ ‘id’, ‘created_at’, ‘updated_at’ ]
table_name.constantize.columns.each do |c|
output << “#{c.name}:#{c.type}” unless ignore_columns.include?(c.name)
end
puts output.join(" ")

returns: rails g scaffold Post --skip-migration title:string body:text
published_at:datetime

On Thu, Oct 20, 2011 at 8:20 AM, genterminl
[email protected] wrote:

Tim,

Thanks. That looks like exactly what I need to get started. I’m simply not
familiar enough yet with ruby or rails to have come up with that from
scratch myself, but I can certainly use it and modify it to handle the
naming conventions in my existing schema. Now I have to go back to working
on updating Dr. Nic’s magic_model_generator to work with rails 3.

this gem might also have things you can use
https://github.com/amatsuda/hocus_pocus

Tim,

Thanks. That looks like exactly what I need to get started. I’m simply
not
familiar enough yet with ruby or rails to have come up with that from
scratch myself, but I can certainly use it and modify it to handle the
naming conventions in my existing schema. Now I have to go back to
working
on updating Dr. Nic’s magic_model_generator to work with rails 3.