Access Migrations via console in Ruby on Rails - ruby-on-rails

I am new to Ruby on Rails . This might be a very foolish question.
I have created a migration using
rails generate migration Kapol name:string position:integer
rake db:migrate
Then using phpmyadmin i copied the database already present
I then opened up rails console
My question is can i use the method Kapol.find(1)??
because when i tried it using singular or plural it says
unitialized constant:Kapol
I know that there has to be a method but where to specify it?

You must generate a model in case to create a table for it, because the migration is usually used to modify existing tables.
It might be confusing that the model generator also creates migration file in your migrations folder. The only difference is that the model generator also generates initial code to create table, on the other hand, the migration generator creates only migration file without initial code.
rails generate model Kapol name:string position:integer
More information: http://guides.rubyonrails.org/getting_started.html#generating-a-model

If you're very new to Ruby on Rails, probably the best thing for you to do is create a scaffold, which gives you your migration file, your model file, your controller file, and various view files, test files, etc. etc., which all work well together. Then you can play with these and build up from there.
rails generate scaffold Kapol name:string position:integer
If you're happy with the migration that was automatically generated, then rake db:migrate and you're all set.
As Andrew says below, you can also just generate any of those files one at a time by replacing 'scaffold' with 'model', etc.
Your Kapol.find(1) is correct.

Related

Ran code generator: rails generate scaffold post title:string body:text and am getting error message

I'm getting this error message when I run the above code generator: (I'm just a beginner)
invoke active_record
Another migration is already named create_posts.....
Use --force to remove the old migration file
What do I type into the terminal window to "use force"
You are getting the following error because you already have a migration named create_posts in your rails application.
invoke active_record Another migration is already named create_posts..... Use --force to remove the old migration file
So, what you need here is first remove the existing migration and then generate the scaffold.
rails d migration create_posts
rails generate scaffold post title:string body:text
Or
You could generate the scaffold using --force option
rails generate scaffold post title:string body:text --force
EDIT
As per your comment:
I did that and then a whole bunch of code appears with the lines of
code sating invoke...exist...identical.
It means that you already ran a scaffold once for Post successfully and you are trying to generate the scaffold again.
I am not sure why you are doing that BUT identical is not an error. Its just that Rails is telling you that you already have a particular file so I am not creating again.
You can reset your database if you don't care about losing your database with this :
b rake db:reset
This will re-reun all your migrations. But take note! Your migrations should be able to run from one end to the other. So if something is "not working" with the regular rake db:migrate, then you should resolve that issue is specifically.
Show me a more descriptive error, and I can tell you more.
You should add other migration in order to change your Post table as you want it to be.
Your could begin with rails g migration and see the help provided.
If you want to get away with it you can delete the migration that created the Post table (but I guess you would need to delete the DB)
After the first time you generate a scaffold, by default Rails will not overwrite the existing scaffold. This is to ensure that you don't accidentally destroy a lot of work.
If you're really sure you want to regenerate the scaffold and delete any changes you might have made to any of the generated files, try:
rails generate scaffold post title:string body:text --force

Delete model and migration file rails 4

I was attempting to add a tagging model to my rails blog. But I accidentally generated a "Tags.rb" model as opposed to "Tag.rb", after reading the guide I realized that making "tag" plural was a mistake when it comes to model. I rolled back the migration using
rake db:rollback
and then
rails destroy model Tags.rb
and this was what i got back
invoke active_record
remove /home/migration/templates/create_table_migration.rb
remove app/models/tags.rb.rb
invoke test_unit
remove test/models/tags.rb_test.rb
remove test/fixtures/tags.rbs.yml
When I come back to the model folder though its still there.
Please help :)
Just give
rails destroy model Tag
Try using tags plural without the additional .rb, like
rails destroy model Tags
Hope this helps
You can manually delete the files, or just manually remove the "s"s.
try this
bundle exec rake db:rollback
and then delete your model as
rails destroy model <model_name>
now totaly deleted.
You can delete the migration files in migrate folder and model in your model folder and then you can again create new one which you wants.
you have to run that commands
rails g model tag
Then migrate database
rake db:migrate
First off, when generating a Rails model using rails generate model, don't add .rb to the end of the model name. Rails automatically does that so if you add .rb yourself then you end up with a model named Tag.rb.rb, which is invalid.
You're running the remove generator with "Tags.rb". Did you generate the model originally using rails generate model Tags.rb? If you did, then running rails destroy model Tags.rb will hopefully remove all the files, but since the model name is invalid, it might not work as expected (your use case is outside the intended use case of the generator). If you originally generated the model using rails generate model Tags, then running rails destroy model Tags should work.
If you originally invoked rails generate model Tags.rb, and running the remove command isn't working (which again, in understandable given that Tags.rb is an invalid name--would have been nice if the generator had originally prohibited you from generating a Tags.rb.rb file, but you can't have everything):
If you can afford to destroy your database and rerun all the migrations, the easy solution will be to manually remove the added files, destroy the database, and rerun all migrations. Then you can add the correct model using rails generate model Tag.
If you can't afford to destroy your database, then you'll need to manually find the problem migration in the migration's folder, run the down command on that migration, and then manually delete that migration file and all the other files.

invoke active_record error

I'm trying to generate a Post model in rails using the following code:
rails g model Post url:string, title: string
When I execute that line I get the following output:
invoke active_record
Another migration is already named create_posts: /Users/myname/Desktop/folder/my_project/db/migrate/20121212021831_create_posts.rb
It seems to be expressing a conflict as though the file already exists in my Model folder - which it does not.
Is this a naming problem? Any thoughts?
The conflicting migration would be in your db/migrate folder, not app/models.
Your two options are naming your new migration something else or removing the old migration. If you choose to remove the old migration, make sure to roll it back first before deleting it, so that your database schema is correct.
This problem can happen when you execute rails g model post for multiple times. You can resolve this issue by executing the rails destroy model post to remove last generated content.

How does these rails generate command differ? and what basically rails generate means?

rails generate migration
rails generate model
rails generate scaffold
rails generate controller etc.
How these differ?
According to rails guides:
Using generators will save you a large amount of time by writing boilerplate code, code that is necessary for the app to work, but not necessary for you to spend time writing. That’s what we have computers for.
rails generate commands family used to provide simple and easy way for developer to create different object types.
rails generate migration - creates DB migration script in db/migrations directory so developer can setup his DB.
rails generate model - creates model class with associated migration, test and fixtures (test data).
rails generate scaffold - creates all nedded classes with basic logic and presentaion. It creates controller (with simple CRUD logic), model, fixtures, functional and unit tests.
rails generate controller - creates controller with associated functional tests, helper and basic views templates.
You can read more here: http://guides.rubyonrails.org/command_line.html#rails-generate
They differ in the sense that they generate different stuff.
migration will generate a database migration file,
model will generate a model(with a migration and a spec by default)
scaffold will generate a scaffold of a resource
and controller will generate a controller.
generate means it will create the files for you with boiler plate code already in place(you will still need to edit them though..but scaffold can get you working with a basic application already)
Read more about it here: http://guides.rubyonrails.org/command_line.html#rails-generate
rails generate is a command line script for quickly generating the code for various Rails' constructs.
In the example you give they differ by what they produce, with the first argument being the type of code generated. For example if I wanted to create a User model I would run:
`rails generate model user`
The model file, test file and migration would be created for me.
You should read the Rails' documentation to find more.
**rails generate model user:
The above command create a Template Object that is a mirror image of the database table.
For example, if you have a database table that is named users that has a name:string, and email:string field,then "rails generate model user" create an Object that mirrors that user table with a few addition.
Here are the similarity they both have name:string,email:string the both have the word user.
The difference are few but significant: user is Capitalized in the model name like "User".
The Table add create_by and updated_by automatically.
migration:db create the database mirror using the model as a model.RECURSION ANYONE?

Model from existing table in Rails 2

I have a database with tables. I want to create a model in my Rails app from existing table. As i know, such functionality is available, and is done as follows:
script/generate scaffold model_name --skip-migration
Of course, i defined my database in database.yml file. Scaffold generated for me a model with controller and views. My table name is not as it must be for Rails(it is incorrect, not following conventions), i added set_table_name to my controller. But, when i am calling the index method, on my page i have only set of # symbols, but not a data from database. In my index.html.erb i have only generated code by scaffold. How can i print out my database data?
Have you generated a schema file from your existing database? If you run the command
rake db:schema:dump
and then re-generate your scaffold this should fix the problem.
Additionally you may wish to check out Dr Nic's Magic Model generator. This will generate models for all of your existing tables and attempt to guess the relationships. This will probably not work if your table naming is not understandable by rails.
UPDATE
I do not generally use the default scaffold however I have tested this myself and it appears that if you skip the migration and do not pass any column name/type pairs then the scaffold generator will not create anything in the template to render the columns.
You have two choices here either
Pass in the column name pairs as well as skip-migration or
Download Ryan Bates Nifty Scaffold generator which will create the scaffold with the column names even if you specify --skip-migration

Resources