how to create new rails generator for dropping a table - ruby-on-rails

I creating an app that needs to be able to create and drop tables on the fly, on create it's pretty simple I use:
rails g model User name:string
but there is no generator for dropping a table.
I want to make it be possible to:
rails g migration DropTableUser

To correct your question, rails g migration User name:string would result in a migration called User not a model, I guess what you meant was rails g model User name:string.
To answer your question, I think it would result in an unnecessary mental effort, especially since something like this already exists in:
rails [d|destroy] model User
If you want to still implement yours, I'd advise that you look at Rails Generators instead for a way to help you out. I guess that would be dependent on creating a migration template etc.

Related

Is there a way to set up model associations when you generate a migration?

New to rails and wondering if there is a way to have all the associations you generate from a migration show up in that model. For example, working on a mock AirBNB app. If i were to generate a migration like so (with these models already created)
rails g migration AddListingsToNeighborhood listing:belongs_to
When I run rake db:migrate, my models are still empty. Just wondering if there is a shortcut to have these associations fill the models.
Thank you!
If you have not created the model (or can overwrite it), use
rails g model Neighborhood listing:references
If you have the model and just need to create the appropriate migration:
rails g migration AddListingsToNeighborhoods listing:references
The DB migration will not modify the model.
In these examples, Neighborhood gets the foreign key.
If you want Listing to have the foreign key (and have belongs_to), then you would need to reverse them:
rails g model Listing neighborhood:references
rails g migration AddNeighborhoodToListings neighborhood:references
FWIW, as you grow your apps, you will often be modifying existing models and adding relationships. This means manually adding belongs_to and running the migration.

Command to create database tables(with fields and relationship) based on model

Is there a way to specify the fields and relationships in the model and run a command to the rails create tables in the database based on the models, and then I can create or remove other fielda, change some relationships and run this command again and rails do update the tables in the database? I thought the migrate did it but after I read about it seems that it only creates the tables once and has no concept of model / db sync that i need.
No you need to create a new migration anytime you need to update the database. Changing some code in the model will not generate a migration.
No, but if you want a simpler way to write your migrations etc, I suggest you read more about the generators :)
For example :
rails generate model user first_name:string last_name:string birth_date:datetime friend:references
will directly generate the migration with the correct fields :)
And if you want to add a field after, you can write it like :
rails generate migration AddPseudoToUsers pseudo:string
And it will be correctly written normally :)
This is the doc
http://guides.rubyonrails.org/migrations.html#creating-a-migration
http://guides.rubyonrails.org/command_line.html#rails-generate

What's the proper way to create a database referencing a user using the devise gem?

I'm creating a simple webapp that tracks calories as a way to learn Ruby on Rails. I'm using the gem devise for users to sign in. My next step is to generate a model. I was going to use:
rails generate model Tracker calories:integer consumed_on:datetime
The problem though is that I don't know how to relate this data to the signed in user. What am I missing from this generate model command?
rails generate model Tracker user_id:integer calories:integer consumed_on:datetime
This will generate your model with a foreign key in the database referencing the user table.
Of course this is not all you have to do, you have to put other code in your model and controller to combine those two columns, create the views, update the config/routes.rb file ..
Also:
Don't forget to run the rake db:migrate to make the relational table in the database.
You can see how its done here (your Tracker model/table is theirs Micropost):
http://ruby.railstutorial.org/chapters/a-demo-app#sec-microposts_resource

Is there a way of modyfiong a new Rails model without migrations?

So i'm making a Rails application to learn how to use the framework.
I created a user using:
rails g model User name:string
Then I realized my user also needed another attribute and a relationship with another resource called user_role.
Say my UserRole model is something like:
rails g model userRole roleName:string
So far I've found that I can only achieve the change using a migration, but this is rather inconvenient and unclear to me, since I don't really have any old data, I'm just adjusting my model.
Any advise for a noob?
Migration is extremely helpful. It keeps track of your schema changes. However, if you find that you forgot to add an field immediately after creating migration, you can edit the old migration without creating a new. If you already migrated your database (using rake db:migrate) you can rollback.
rake db:rollback
Now, edit the migration file and add/remove you fields. When completed, migrate again
rake db:migrate
When you create a separate model, you SHOULD create a separate migration. Otherwise, in future you will be confused yourself. btw, when creating children model, you don't need any change in the parent migration. you need to add foreign key in the child table. you can use something like this:
rails g model Comment post:references
this will add post_id in the comment table.
You could try a non-database backed ActiveModel-like model. For example, Mongoid or Datamapper. Many NoSQL solutions don't require migrations for changes to models.
Try this:
rails g model ModelName --migration=false

Rails model generator - from existing database

Is there any working automatic model generator for creating a model from existing database?
Something like symfony's task symfony doctrine:build-model.
I found just Dr Nic’s magic model generator but it doesn't work with rails 2.3+. Please do not recommend Dr Nic's magic models. That's not what I want.
I don't believe that there is nothing else for such a common task.
EDIT: I don't want to generate just empty models. I want to also automatically generate associations and validations.
Hey there. This is quite a simple procedure to do;
Modify your_project/config/database.yml with connection params so that it connects up to your existing database.
For each table you wish to turn into a model type the following:
script/generate model tablename

Resources