ruby model naming conventions can uppercase characters be used - ruby-on-rails

I have run a migration and am getting a 500 error saying seen_by_hw does not exist. Could this be due to the fact I have used an uppercase characters when creating the ruby model. eg
seen_by_HW. Or should it be seen_by_hw
thanks

If you created the model file/class yourself then yes, this is probably the issue.
The filename should be: seen_by_hw.rb and in that the class name will be SeenByHw.
If you use the generators that comes with Rails then it would have done this for you, type rails generate in your app directory to see more info on them.

Related

How to get the file name of a new migration file using Ruby syntax

So, one of the most frustrating things for me is trying to get the dynamically named migration file after it is created in order to manipulate it in a Ruby on Rails application template.
The problem arises when I run something like run "rails g migration CreateSettings" and then want to replace the created migration file with a pre-made migration file that has default values and null: false etc spelled out. It is quite difficult to know exactly what the file name is as the dynamic timestamp is down to the second.
I did a lot of research through StackOverflow and online and many people kept pointing to doing a Dir.glob to find the file, but the problem with that is that the name is dynamically created and if I have multiple migration files in there, I need to make sure that it is grabbing the correct file to replace.
QUESTION:
What is the best way with Ruby syntax to get the full file name of a dynamically created migration file following a rails g migration call? Say that the file was created after calling run "rails g migration CreateSettings", thus the dynamic name will follow the naming syntax: YYYYMMDDHHmmss_create_settings.rb
VERSIONS
Using Rails -v 6.1.3
Using Ruby -v 2.7.1p83
After a lot of research and trial and error, this is the solution that I came up with.
After running run "rails g migration CreateSettings" I then run this Ruby Code:
1. Dir.chdir('db/migrate/') {
2. Dir.glob('*.rb').each {|filename|
3. if filename.include? "create_settings"
4. file_location = 'db/migrate/' + filename
5. end
6. }
7. }
What this code does is:
(line 1) Changes the directory to look in to the migration folder.
(line 2) Within that directory it finds all files with an rb extension and starts to go through them one at a time (the each statement) with every iteration giving the new filename to the variable filename
(line 3) Checks to see if the filename string includes the string "create_settings"
(line 4) If it does include that string, it saves a new variable for the file path (location)
(lines 5-7) closes the circuits ;-)
Since there should only be one migration file for creating settings, this works. For anyone else using this in the future, make good use of naming conventions. For instance, if you add a new field for the Rails app name to the settings table, you should run the migration "AddAppNameToSettings" NOT "AddFieldToSettings". Because if you do this again and have once again have the second migration name of "AddFieldToSettings", you will have two migration files with different timestamps but with "add_field_to_settings" in the title and the above code will find both files.
I hope that my research and time can be of help to others in the future saving you the time it took me to research this.
Happy coding!

Rails generate model --orm option missing

When I try to generate a model using the next sentence:
rails g model SupplyRequestProfile name:string{50} active:boolean
It fails, and shows the next single line
No value provided for option '--orm'
But, when I use, skipping the fields and types:
rails g model SupplyRequestProfile
It does the job and all is normal.
I have looked if I use a reserved word of rails for my propertys in http://reservedwords.herokuapp.com/ but there is no one that im using.
The problem is not the config/application.rb, all is ok there.
So, I dont know what could be happen here, but it is like some of my property names or types are wrong or causing some strange behavior on rails generation.
What could be wrong?
Simply putting the fields names with their types inside double quotes will work:
rails generate model SupplyRequestProfile "name:string{50}" "active:boolean"

Generate Rails Model named "Signal"

I tried to generate a rails model like so:
rails generate model signal
Which produced this error:
The name 'Signal' is either already used in your application or reserved by Ruby on Rails. Please choose an alternative and run this generator again.
I quickly discovered that "Signal" was a reserved name for models:
Reserved names with ActiveRecord models
Is there any way around this so I can have a model named "Signal", like wrapping the model in a custom Module?

Add column to migration when using compound model name?

I have a model called MyArticle. When I try to use the command
rails generate migration AddtestToMyArticle test:string
the migration file contains only empty up/down methods. Having done this previously on a single word model name, it worked just fine and the migration up/down methods had the appropriate code.
I tried "AddtestToMy_Article" but that didn't work either. What do I need to do to work with my compound model name and the generate migration command?
You need to use
rails generate migration AddColumnToMyArticle test:string
When using 'AddColumn' you will have the appropriate code in your migration.
I just looked back over this, and while my answer is correct, it's better to have a more descriptive migration name. The user below who noticed the capitalization is right in that if you don't have each new word capitalized, Rails won't pick up on what exactly you're trying to do. So, in your question you have AddtestTo... but it should be AddTestTo....
It seems to work if you use underscores rather than CamelCase
rails g migration add_test_to_my_article test:string
Hope this helps
rails generate migration AddNewFieldToMyArticle new_field:string

Where are the docs for Rails "script/generate model"?

I am running
ruby script/generate scaffold
or
ruby script/generate model
and I know the basic syntax, like
ruby script/generate scaffold Dude name:string face:boolean
but I do not know things like:
should names of variables have underscores or be camelCased?
what kind of variable types are acceptable?
Where can I find such information?
Type the command without arguments and the documentation is revealed:
$ script/generate model
You can use either camelcase or underscores for the model name.
Model names are singular; controller names are plural.
Field names use underscores.
I can't remember all the possible field types, I just look them up from the Migration docs, as linked above.
This document on Rails Migration would help.
With respect to the naming convention, I think the general adopted convention for Ruby on Rails is to have underscores.
To know which variable types are acceptable, refer to the section on Database Mapping.
There is a resource on the rails wiki as a List of Available Generators.
To check Rails naming conventions, topfunky's Pluralizer was useful.
there is a new syntaxis for Rails is rails generate

Resources