Generate model from database

25 views
Skip to first unread message

prasetya utama

unread,
Oct 18, 2011, 10:01:17 AM10/18/11
to rubyonra...@googlegroups.com
hello i want to ask, how to generate model from table in my database.
That's possibel ?

--
Posted via http://www.ruby-forum.com/.

Colin Law

unread,
Oct 18, 2011, 10:15:57 AM10/18/11
to rubyonra...@googlegroups.com
On 18 October 2011 15:01, prasetya utama <li...@ruby-forum.com> 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

prasetya utama

unread,
Oct 18, 2011, 10:59:49 AM10/18/11
to rubyonra...@googlegroups.com
@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 ...)

Colin Law

unread,
Oct 18, 2011, 11:09:12 AM10/18/11
to rubyonra...@googlegroups.com
On 18 October 2011 15:59, prasetya utama <li...@ruby-forum.com> 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

prasetya utama

unread,
Oct 18, 2011, 11:25:13 AM10/18/11
to rubyonra...@googlegroups.com
yes i want to generate model, but the table has been created in
databases. And i want generate that table :)

Colin Law

unread,
Oct 18, 2011, 11:40:04 AM10/18/11
to rubyonra...@googlegroups.com
On 18 October 2011 16:25, prasetya utama <li...@ruby-forum.com> wrote:
> yes i want to generate model, but the table has been created in
> databases. And i want generate that table :)

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

prasetya utama

unread,
Oct 18, 2011, 11:53:17 AM10/18/11
to rubyonra...@googlegroups.com
Thanks colin, for your advice :) and your coment

prasetya utama

unread,
Oct 18, 2011, 11:58:16 AM10/18/11
to rubyonra...@googlegroups.com
Colin Law wrote in post #1027233:

Thanks colin, for your advice :) and your coment

--
Posted via http://www.ruby-forum.com/.

genterminl

unread,
Oct 18, 2011, 5:05:59 PM10/18/11
to Ruby on Rails: Talk
Oct 18, 11:40 am, Colin Law <clan...@googlemail.com> wrote:
> On 18 October 2011 16:25, prasetya utama <li...@ruby-forum.com> wrote:
>
> > yes i want to generate model, but the table has been created in
> > databases. And i want generate that table :)
>
> 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.
>
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?

Paul

unread,
Oct 19, 2011, 12:39:30 PM10/19/11
to rubyonra...@googlegroups.com
On Tue, Oct 18, 2011 at 5:05 PM, genterminl
<ostr...@users.sourceforge.net> 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?

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?

Tim Shaffer

unread,
Oct 19, 2011, 12:47:47 PM10/19/11
to rubyonra...@googlegroups.com
I believe you can also pass a "--skip-migration" option to scaffold so it won't generate the migration.

genterminl

unread,
Oct 19, 2011, 4:16:08 PM10/19/11
to rubyonra...@googlegroups.com
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.)

Doug

unread,
Oct 20, 2011, 12:13:20 AM10/20/11
to Ruby on Rails: Talk


On Oct 19, 4:16 pm, genterminl <ostrof...@users.sourceforge.net>
wrote:
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.

Tim Shaffer

unread,
Oct 20, 2011, 10:37:06 AM10/20/11
to rubyonra...@googlegroups.com
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

genterminl

unread,
Oct 20, 2011, 11:20:01 AM10/20/11
to rubyonra...@googlegroups.com
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.

Noel

unread,
Oct 20, 2011, 12:24:50 PM10/20/11
to rubyonra...@googlegroups.com

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

>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/rubyonrails-talk/-/C6b9xZ_D5rsJ.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To unsubscribe from this group, send email to
> rubyonrails-ta...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/rubyonrails-talk?hl=en.
>

Reply all
Reply to author
Forward
0 new messages