hello i want to ask, how to generate model from table in my database. That''s possibel ? -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 18 October 2011 15:01, prasetya utama <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
@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 ...) -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 18 October 2011 15:59, prasetya utama <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
yes i want to generate model, but the table has been created in databases. And i want generate that table :) -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 18 October 2011 16:25, prasetya utama <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Thanks colin, for your advice :) and your coment -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin Law wrote in post #1027233:> On 18 October 2011 16:25, prasetya utama <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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. > > ColinThanks colin, for your advice :) and your coment -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Oct 18, 11:40 am, Colin Law <clan...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> On 18 October 2011 16:25, prasetya utama <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Tue, Oct 18, 2011 at 5:05 PM, genterminl <ostroffjh-Rn4VEauK+AKRv+LV9MX5uipxlwaOVQ5f@public.gmane.org> 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? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
I believe you can also pass a "--skip-migration" option to scaffold so it won''t generate the migration. -- 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/-/grjZs4PNm6sJ. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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.) -- 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/-/58HJ3GSeLWMJ. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Oct 19, 4:16 pm, genterminl <ostrof...-Rn4VEauK+AKRv+LV9MX5uipxlwaOVQ5f@public.gmane.org> 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. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 -- 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/-/q3wXc3R5y8kJ. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Thu, Oct 20, 2011 at 8:20 AM, genterminl <ostroffjh-Rn4VEauK+AKRv+LV9MX5uipxlwaOVQ5f@public.gmane.org> 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> > -- > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.