hi i m new to rails i need to know the => which is the best way to create Database table in Rails? 1) *rails generate model User name:string email:string* this will create a migration file with something like this class CreateUsers < ActiveRecord::Migration def change create_table :users do |t| t.string :name # *see here name column is already made becoz we have passed a parameters* t.string :email # *same as above* t.timestamps end endend this is one method 2) *rails generate model Page* this will create a empty migration file something like this class CreatePages < ActiveRecord::Migration def change create_table :users do |t| ===>> *now we can add here like t.string "name" to create column manually* ========>>>>> t.timestamps end endend So which is the good way to do above things example 1 or example 2? Please tell. and if i have to write a drop_table method in migration, can i write in the way which i showed below class CreateSubjects < ActiveRecord::Migration def change create_table :subjects do |t| t.string "name" t.timestamps end end def self.down drop_table :subjects end end So again same question *which is the good way to do above things example 1 ( model with parameter ) or example 2 ( model without parameter )?* Thanks for Taking Time to Read. -- 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 To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/WsDhrCEK-u8J. For more options, visit https://groups.google.com/groups/opt_out.
please help me On Sat, Sep 8, 2012 at 2:38 AM, ACK <aniketkadam1992-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> hi i m new to rails i need to know the => which is the best way to create > Database table in Rails? > > 1) *rails generate model User name:string email:string* > this will create a migration file with something like this > > class CreateUsers < ActiveRecord::Migration > def change > create_table :users do |t| > t.string :name # *see here name column is already made becoz we have passed a parameters* > t.string :email # *same as above* > > t.timestamps > end > endend > > > this is one method > > 2) *rails generate model Page* > this will create a empty migration file something like this > > class CreatePages < ActiveRecord::Migration > def change > create_table :users do |t| > ===>> *now we can add here like t.string "name" to create column manually* ========>>>>> > t.timestamps > end > endend > > > So which is the good way to do above things example 1 or example 2? > Please tell. > > and if i have to write a drop_table method in migration, can i write in > the way which i showed below > > class CreateSubjects < ActiveRecord::Migration > def change > create_table :subjects do |t| > t.string "name" > > t.timestamps > end > end > > def self.down > drop_table :subjects > end > end > > So again same question *which is the good way to do above things example > 1 ( model with parameter ) or example 2 ( model without parameter )?* > Thanks for Taking Time to Read. > > -- > 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 > To view this discussion on the web visit > https://groups.google.com/d/msg/rubyonrails-talk/-/WsDhrCEK-u8J. > For more options, visit https://groups.google.com/groups/opt_out. > > >-- 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 https://groups.google.com/groups/opt_out.
Rails is smart enough to determine if you want to create or drop a table if you have done the migration with change def change #code here end it only depends on how you write the commands if you do rake db:migrate then it will create the db (if the code inside change is about it) and if you do rake db:rollback it will drop that table in other case then def up #create table here end def down #drop table here end take a look here http://guides.rubyonrails.org/migrations.html and then here http://stackoverflow.com/questions/10365129/rails-migrations-self-up-and-self-down-versus-change http://stackoverflow.com/questions/7600415/rails-3-generate-migration-no-up-or-down-method there is a lot more on google JavierQ -- 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 https://groups.google.com/groups/opt_out.
use def change forget up and down def cheers Fahim Babar Patel On Sat, Sep 8, 2012 at 9:55 AM, Javier Quarite <jquarites-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Rails is smart enough to determine if you want to create or drop a table > if you have done the migration with change > def change > #code here > end > > it only depends on how you write the commands > if you do > rake db:migrate > then it will create the db (if the code inside change is about it) and if > you do > rake db:rollback > it will drop that table > in other case then > > def up > #create table here > end > > def down > #drop table here > end > > take a look here > http://guides.rubyonrails.org/migrations.html > and then here > > http://stackoverflow.com/questions/10365129/rails-migrations-self-up-and-self-down-versus-change > > http://stackoverflow.com/questions/7600415/rails-3-generate-migration-no-up-or-down-method > there is a lot more on google > > JavierQ > > -- > 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 https://groups.google.com/groups/opt_out. > > >-- 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 https://groups.google.com/groups/opt_out.
thanks for reply On Sat, Sep 8, 2012 at 9:55 AM, Javier Quarite <jquarites-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Rails is smart enough to determine if you want to create or drop a table > if you have done the migration with change > def change > #code here > end > > it only depends on how you write the commands > if you do > rake db:migrate > then it will create the db (if the code inside change is about it) and if > you do > rake db:rollback > it will drop that table > in other case then > > def up > #create table here > end > > def down > #drop table here > end > > take a look here > http://guides.rubyonrails.org/migrations.html > and then here > > http://stackoverflow.com/questions/10365129/rails-migrations-self-up-and-self-down-versus-change > > http://stackoverflow.com/questions/7600415/rails-3-generate-migration-no-up-or-down-method > there is a lot more on google > > JavierQ > > -- > 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 https://groups.google.com/groups/opt_out. > > >-- 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 https://groups.google.com/groups/opt_out.
On 7 September 2012 22:08, ACK <aniketkadam1992-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> hi i m new to rails i need to know the => which is the best way to create > Database table in Rails? > > 1) rails generate model User name:string email:string > this will create a migration file with something like this > > class CreateUsers < ActiveRecord::Migration > def change > create_table :users do |t| > t.string :name # see here name column is already made becoz we have > passed a parameters > t.string :email # same as above > > t.timestamps > end > end > end > > > this is one method > > 2) rails generate model Page > this will create a empty migration file something like this > > class CreatePages < ActiveRecord::Migration > def change > create_table :users do |t| > ===>> now we can add here like t.string "name" to create column > manually ========>>>>> > t.timestamps > end > end > end > > > So which is the good way to do above things example 1 or example 2? > Please tell.It is entirely up to you, do whichever you prefer. 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 https://groups.google.com/groups/opt_out.