1

I'm new to rails...I made a scaffold "Reviews" but want to add another field "ratings", as an integer. I did the following:

added t.integer :ratings in the migration file..ran rake db:migrate

in spec folder: added it in views/app/ edit, index, new, show

in app/views/app added it in the json files

in app/controllers/app added it in the review_params function

still however whenever I try to reference (by showing a Review) it I get

undefined method `ratings' for #

There must be something else I need to add somewhere to have it be part of my Reviews scaffold. I've been trying to figure it out for 5 hours but still have not. When I try to remake a scaffold and run rake db:migrate I get an error saying that the databases already exist so I would like to just manually add it to my existing one if possible, I just can't seem to figure out how even though I've already done it once for a string.

Any help is appreciated thank you.

parameter
  • 894
  • 2
  • 18
  • 35

3 Answers3

2

To add an integer field to a model you can do something like this.

rails generate migration AddRatingToReviews rating:integer

This should handle everything for you by generating a new migration file like so.

class AddRatingsToReviews < ActiveRecord::Migration
  def change
    add_column :reviews, :rating
  end
end

Then you can run rake db:migrate to add the column to your review model.

NOTE: Before doing all of this, please delete all your manual changes. If necessary use rake db:rollback which will rollback your most recent rake db:migrate.

Recommendation

If you are new to rails and don't understand MVC, I suggest not using scaffolding because you'll have a tough time knowing what it is doing. Go through this awesome tutorial by Michael Hartl to really learn rails quickly. http://ruby.railstutorial.org/

*Awesome gem *

Use the annotate gem to display the attributes contained within your model directly in your name_of_model.rb files.

https://github.com/ctran/annotate_models

Derrick Mar
  • 1,021
  • 1
  • 12
  • 15
  • will it handle everything in the view files and controller? or will i need to manually add "ratings" to those still? – parameter Mar 16 '14 at 23:00
  • I think you are not conceptually understanding what models are. Take a look at here http://stackoverflow.com/questions/1931335/what-is-mvc-in-ruby-on-rails. – Derrick Mar Mar 16 '14 at 23:04
  • I understand the concept of MVC and have been following that tutorial. The problem I'm having is not with migration of the new file or field, it's that the Review does not seem to actually have a ratings field, despite me manually adding it to the other files as I did with another field that did work. Referencing the field results in the error saying that the method is undefined. – parameter Mar 16 '14 at 23:40
  • for example when I try to create a new Review and have a div for ratings, I get an error saying ratings is an undefined method for review. – parameter Mar 16 '14 at 23:46
  • what do you mean by manually adding it to the other files? You shouldn't need to do that. Just use the rails generate command as stated above. Do this: 1. Go to rails console by typing 'rails c' 2. See your list of attributes in your review model by typing 'Review.attribute_names' . Do you see your attribute there? – Derrick Mar Mar 17 '14 at 01:15
1
rails generate migration AddRatingsToReviews ratings:integer

Then

rake db:migrate
PrivateUser
  • 4,474
  • 12
  • 61
  • 94
0

If you have not done any major changes in your generated scaffold.

Simplest way to get the ratings across views would be as below:

  • Rollback the changes that you have migrated

rake db:rollback VERSION=version_number

Where replace version_number with the version_number prefixed on your migration file.

For eg: If your migration filename is 20140314190622_create_reviews.rb then command should be

rake db:rollback VERSION=20140314190622
  • Destroy the scaffold of Review

rails d scaffold Review

  • After this generate the scaffold again with the integer field

rails g scaffold Review ratings:integer .... ## Add other field in place of ....

Kirti Thorat
  • 52,578
  • 9
  • 101
  • 108