StatementInvalid : Could not find table while creating model object - ruby-on-rails

I've just started programming using Instant rails and don't know much about it. So, I've made a rails project and created a database name contactlist_development and a table named contacts with some fields. I've generated a model named Contact and started the rails console using "ruby script/console".
But when i tried making a new Contact model object, it displays StatementInvalid : could not find the table 'contacts'.

Running rake db:migrate will execute the code you generated, hence creating the table.
Hope this helps!

Have you used rails generate model contact command to make Contact model? If yes, you forget to run command :
rake db:migrate
Or you can make table directly by creating migration or using sql query.

Related

how to access table from rails console if the table has _ in its name

I am trying to access all records of a table that has an underscore in its name.
For example if I have table in my schema that is called trips I can do Trip.all in rails console. But what do I do if my table name contains an underscore (e.g. users_foods)
I tried the following options:
Users_food.all
User_food.all
User_foods.all
etc.
All of the above did not work, any suggestions?
Figured it out
One can access the data with UserFood
For a table named users_foods, ActiveModel should provide you with a corresponding Rails model UsersFood, to fit Ruby/Rails object naming convention. Try that.
Class names Users_food or User_food, etc. do not conform to Ruby convention.
I had the same problem when creating bonus_histories table. And didn't work any of the answers until I found out why.
I have made only rails g migration BonusHistory
and it was the problem. My rails console didn't find BonusHistory at all because I had no model.
So I had to first rollback the migration rake db:rollback STEP=1, then deleted the migration file and finally made
rails g model BonusHistory
and after migrating that table, when I enter rails console, I can successfully ask for BonusHistory.count

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 to add a new instance variable to a class in ruby on rails using rubymine?

I have created a class using scaffold in rubymine and did db migrate. Now I need to add one more instance variable (one more column to table in db) to the same class. How do I do this using rubymine (not from command-line) without destroying the class?
I don't think I understand the real issue here.
Would you not just rollback the current migration modify the migration in db/migrate (N) and add the column you want, then update the views for the model (since you've used a scaffold)? Once you do that, you would migrate again. Either that, or you'd create a new migration that adds the column you want– however, given that you've obviously just started this app, I see no reason to add an additional migration simply for this.

Access Migrations via console in 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.

RoR NameError - Ruby on Rails MySQL

NameError in GenresController#index
uninitialized constant GenresController
RAILS_ROOT: C:/Users/Will/Desktop/INSTAN~1/rails_apps/talewiki
I've created a table called Genres and when I try to connect to it via local host I get the above error.
Any ideas?
With all the questions you're asking I believe you're an absolute beginner regarding ROR. Perhaps you should visit some tutorials to learn rails.
I don't know what your genre model describes, but I think it will have a name.
Basic steps for a basic genre model:
Delete the table for your genres if created manually (with SQL code)
DROP TABLE genres;
generate a complete scaffolding for genres:
$ ruby script/generate genre name:string
$ rake db:migrate
Now you have a complete controller for all CRUD actions for a simple genre model
If I were you I would read some tutorial about RoR, because you make the impression that you don't understand RoR or the MVC principle behind it. A good start would be: http://storecrowd.com/blog/top-50-ruby-on-rails-tutorials/
You need to generate a controller to handle the index action when your browse your application on localhost
ruby script/generate controller genres index
run that from your console within your application and it will generate the GenresController with the action index (it will be an empty action but you shouldn't see an error when browsing http://localhost:3000/genres/)
file
C:/Users/Will/Desktop/INSTAN~1/rails_apps/talewiki/app/controllers/genres_controller.rb
must be present

Resources