Creating model and index through one-line command? - ruby-on-rails

I know I can easily create a model with this one line. Now suppose I want to add an index on username. How can I do so with one line without going to edit the migration file manually?
script/rails generate model TwitterUser username:string num_followers:integer num_following:integer bio:string location:string image:string num_tweets:integer website:string

Rails version? New in 3.2, would be this:
rails generate model TwitterUser username:string:index num_followers:integer ...
You use scrip/rails generate so not sure if this helps you

Related

How to make database on ruby on rails?

I have just created the project on ruby on rails. I also generated a controller with an instruction on bash, "rails g controller home".
However, I didn't make database. How can I generate database? Please inform me how to make database.
Thank you.
You can generate a database table by typing:
rails g model myTableName
Because your starting out new i think it would be best to try something like:
rails g scaffold Post name:string content:text
This will link the controller, the model(which is the database) and the views together, giving you a better understanding on how the MVC structure works in rails.

Rails: How to run `rails generate scaffold` when the model already exists?

I'm new to Rails so my current project is in a weird state.
One of the first things I generated was a "Movie" model. I then started defining it in more detail, added a few methods, etc.
I now realize I should have generated it with rails generate scaffold to hook up things like the routing, views, controller, etc.
I tried to generate the scaffolding but I got an error saying a migration file with the same name already exists.
What's the best way for me to create scaffolding for my "Movie" now? (using rails 3)
TL;DR: rails g scaffold_controller <name>
Even though you already have a model, you can still generate the necessary controller and migration files by using the rails generate option. If you run rails generate -h you can see all of the options available to you.
Rails:
controller
generator
helper
integration_test
mailer
migration
model
observer
performance_test
plugin
resource
scaffold
scaffold_controller
session_migration
stylesheets
If you'd like to generate a controller scaffold for your model, see scaffold_controller. Just for clarity, here's the description on that:
Stubs out a scaffolded controller and its views. Pass the model name,
either CamelCased or under_scored, and a list of views as arguments.
The controller name is retrieved as a pluralized version of the model
name.
To create a controller within a module, specify the model name as a
path like 'parent_module/controller_name'.
This generates a controller class in app/controllers and invokes helper,
template engine and test framework generators.
To create your resource, you'd use the resource generator, and to create a migration, you can also see the migration generator (see, there's a pattern to all of this madness). These provide options to create the missing files to build a resource. Alternatively you can just run rails generate scaffold with the --skip option to skip any files which exist :)
I recommend spending some time looking at the options inside of the generators. They're something I don't feel are documented extremely well in books and such, but they're very handy.
Great answer by Lee Jarvis, this is just the command e.g; we already have an existing model called User:
rails g scaffold_controller User
For the ones starting a rails app with existing database there is a cool gem called schema_to_scaffold to generate a scaffold script.
it outputs:
rails g scaffold users fname:string lname:string bdate:date email:string encrypted_password:string
from your schema.rb our your renamed schema.rb. Check it
In Rails 5, you can still run
$rails generate scaffold movie --skip
to create all the missing scaffold files or
rails generate scaffold_controller Movie
to create the controller and view only.
For a better explanation check out rails scaffold
This command should do the trick:
$ rails g scaffold movie --skip
You can make use of scaffold_controller and remember to pass the attributes of the model, or scaffold will be generated without the attributes.
rails g scaffold_controller User name email
# or
rails g scaffold_controller User name:string email:string
This command will generate following files:
create app/controllers/users_controller.rb
invoke haml
create app/views/users
create app/views/users/index.html.haml
create app/views/users/edit.html.haml
create app/views/users/show.html.haml
create app/views/users/new.html.haml
create app/views/users/_form.html.haml
invoke test_unit
create test/controllers/users_controller_test.rb
invoke helper
create app/helpers/users_helper.rb
invoke test_unit
invoke jbuilder
create app/views/users/index.json.jbuilder
create app/views/users/show.json.jbuilder
I had this challenge when working on a Rails 6 API application in Ubuntu 20.04.
I had already existing models, and I needed to generate corresponding controllers for the models and also add their allowed attributes in the controller params.
Here's how I did it:
I used the rails generate scaffold_controller to get it done.
I simply ran the following commands:
rails generate scaffold_controller School name:string logo:json motto:text address:text
rails generate scaffold_controller Program name:string logo:json school:references
This generated the corresponding controllers for the models and also added their allowed attributes in the controller params, including the foreign key attributes.
create app/controllers/schools_controller.rb
invoke test_unit
create test/controllers/schools_controller_test.rb
create app/controllers/programs_controller.rb
invoke test_unit
create test/controllers/programs_controller_test.rb
That's all.
I hope this helps

How to create model rb files for all the tables in Mysql

I wrote a simple migration file that creates around ten tables. It all created perfectly. Now I need to create ten equivalent model files in the app/models folder. I can do it manually. But I am wondering if there is any rake task available to do this.
Tips/advise on this is much appreciated.
Automatically: http://magicmodels.rubyforge.org/magic_model_generator/
Manually: http://forums.devshed.com/showpost.php?p=1957164&postcount=2
You can create a model and migration using the same rails model generator. This will also create a unit test and fixtures.
Rails 2.3.x:
script/generate model Person name:string
Rails 3.0.x
rails g model Person name:string
You can also use the following options (taken from Rails 2.3.8 and might have changed in 3.0)
Options:
--skip-timestamps Don't add timestamps to the migration file for this model
--skip-migration Don't generate a migration file for this model
--skip-fixture Don't generation a fixture file for this model

How to create a nifty_scaffold without worrying with the migrations?

I need to use the nifty_scaffold to generate all the views and controller for my model, but I already have the model, the migration and the table in database, so I don't need it to generate the migrations. The problem is that when it founds an old migration, it says
Another migration is already named your_table: db/migrate/20090904212205_create_your_table.rb
and I can't create the whole scaffold.
Is there any way to ignore migrations on scaffolding in Rails?
script/generate nifty-scaffold MyModel --skip-migration
should work.
Since you already have the model, you can pass the --skip-model parameter. As shown in the railscast tutorial, try this:
script/generate nifty_scaffold user_session --skip-model username:string password:string new destroy

Is there an option to generate scaffolding without generating migrations?

One of the things that bothers me in Rails is that scaffolding generates migrations that I don't always need.
Is there a way to generate the scaffold without the corresponding migration?
Use the --skip-migration parameter. For example:
rails generate scaffold post title:string body:text --skip-migration

Resources