I want to make a scaffold like this:
rails generate scaffold Job started_at:datetime completed_at:datetime price:decimal paid_at:datetime estimated_completion_at:datetime comment:text
I've added "_at" to the datetime fields because that seemed to be the convention as rails timestamp fields are either "_at" for datetime or "_on" for date. But is this actually a convention? Is it OK if I just have "paid" instead of "paid_at" and "completed" instead of "completed_at"?
I really want to get this right from the start. I've been programming in c# for years but I'm new to ruby on rails. I like all the conventions and I want to make sure I'm doing this the proper 'rails way'.
Variable names in Ruby can be created from alphanumeric characters and the underscore _ character. A variable cannot begin with a number. This makes it easier for the interpreter to distinguish a literal number from a variable. Variable names cannot begin with a capital letter.
Naming conventions in Active Record model Rails is capable of pluralizing (and singularizing) both regular and irregular words. Model class names must use the CamelCase form when composed of two or more words, while the database table names must use the snake_case form.
I don't know of any specific rails convention, and it doesn't really matter since there's nothing in rails that keys off of datetime fields. The convention I've always gone with is suffix datetime columns with _at
and date columns with _on
. It always seemed more natural to say on a day and at a time.
How you name your date and time columns is not an officially stated convention and does not affect the way your code works. However, if you want to go »the Rails way« you should probably follow conventions generally accepted in the rails community.
First of all, there is a a community-driven Ruby on Rails style guide, but it doesn't contain any date column naming conventions.
One hint are the Active Record timestamp columns, like created_at
as @sierrasdetandil noted in their answer.
Another way to figure out what some conventions are is by looking at the Rails style guides of popular Rails shops. For example thoughtbot state in their style guide:
- Name date columns with
_on
suffixes.- Name datetime columns with
_at
suffixes.- Name time columns (referring to a time of day with no date) with
_time
suffixes.
This will help you find good names for your columns and prevent confusion for other developers. I think this is a good convention to follow, and you might too, as long as you don't have a compelling reason to name your columns differently.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With