"wrong constant name" error when generating a new model - ruby-on-rails

I'm trying to create a new model like this:
rails generate model Tags name:string
When I run this, I get the following error:
base.rb:266:in `const_defined?': wrong constant name tags (NameError)
Any thoughts? I tried changing Tags to ReportTags and it still gave me the same error.

Model names are singular - try
rails generate model Tag name:string

Related

How to create a table in Ruby on Rails or change the migration order?

I am writing a Ruby on Rails app. I have created a model like so:
rails generate model Post user:references header:string body:text topic:string
And then I created a model User like so:
rails generate model User name:string email:string password:string
But running
db:migrate
Gives me the error:
PG::UndefinedTable: ERROR: relation "users" does not exist
So I guess I messed up with the order of the table creation and should have created the model User first and then migrate before creating the Post model?
You can rename one of the two new migrations in db/migrate/ to change the numeric timestamp in the filename, and thereby change the order they're run.

Rails looking for wrong method

I have a column named _SOMETHING as part of an object created with rails g scaffold, say: rails g scaffold Person _SOMETHING:string
In the create method, when doing #person.save, an error pops up saying that it doesn't find method something(no underscore and lowercases).
Why is it looking for a method with that name?
I patched it by creating
def something
true
end
in my Person model. I am sure that's not the correct way to solve this.
Using Ruby 2.2.2, Rails 4.2.1
Thanks in advance
Turns out the model had a validation where I misspelled the column name, failing each time. Changed the name to _SOMETHING and it worked.
Thank you all for your attention to this silly issue

Setting string limit when creating Postgresql model in command line for Rails App

First question posted on here so all feedback welcome in terms of etiquette etc!
I am trying to create my db model in command line. I am working in Rails 4:
bin/rails g model item name:string description:text price:integer
However, I am looking to put a limit on the name. I think I need
limit:50
however, I cannot get the syntax correct. I keep setting up a new property called '50' which is of type 'limit' or it gets ignored completely. I have tried:
bin/rails g model item name:string:limit:50 description:text price:integer
bin/rails g model item name:(string, limit:50) description:text price:integer
bin/rails d model item name:string limit:50, description:text price:integer
but without success.
Also, cheeky to add on a second part of this question,but Postgresql documentation suggests I can use a type 'money' for the 'price' property but Active Record doesn't like this. Am I right to use 'integer'?
Thanks in advance!
HatStephens
OK, I think I have got this working?
bin/rails g model item name:string{50} description:text price:integer
Maybe this will help someone one day! Or I can search for it when I forget next time!

Tried to create a scaffold but migration already exists in Rails

I tried to create a new scaffold called Message and got this error message:
Another migration is already named create_messages
What can I do aside from changing the name of my model/controller/views (which I don't want to do)?
The reason this is occurring is I had a previous scaffold called Message that I changed to a different name (ran a migration to do this along with a search and replace for all filenames and variable/class names). I understand why I get this error message, just want to know how to move forward.
Quick fix is to rename old migration (create_messages) and try to generate scaffold again.
But recommendation is to have single create_messages migration so that it can be safely deleted by destroy scaffold command.
Update:
If already have Message model then no point in generating Message Scaffold, since scaffold does the same thing again with additional works like generating controller, routes etc.
If you already have a migration called create_messages you probably already have a Message model(Which means you can't have another named the same). If you have now decided you want to have a scaffold on the model to get the extra controller and view code use the following where name is the name of the column.
rails g scaffold Message name --skip

RoR - undefined method `title' for #<Product:0x596c148>

i am new to RoR and following AWDwR book...
all was well till i started the Depot project....
created the products model using
ruby script/generate scaffold product title:string description:text image_url:string
now when i view the page http://localhost:3000/products it shows a generic page with nothing in the list and a link to add new product... when i click this link i get an error..the error is as follows...
undefined method `title' for #<Product:0x596c148>
then i checked the db..it seems there was no title column created at all... did a bit of searching for solutions and have got all the more confused...it appears to be a problem with scaffolding and version problems...
thanks in advance...
Perhaps you still need to run the migration? Try this:
rake db:migrate

Resources