What Are All the "Conventions" for Ruby on Rails

Rails: Naming conventions for attributes

Conventions aren't set in stone, but it's good to follow them.

And yes, widely spread convention for attribute names is snake case.

Snake case:

punctuation is removed and spaces are replaced by single underscores. Normally the letters share the same case (either UPPER_CASE_EMBEDDED_UNDERSCORE or lower_case_embedded_underscore) but the case can be mixed.

Rails model associations naming conventions

You need to rename your model(and table if need) to Selfy, check this example of what the Rails doing under the hood:

$> bundle exec rails console
# convert to table name
"Selfy".tableize
=> "selfies"
# singular
"selfies".singularize
=> "selfy"
# plural
"selfy".pluralize
=> "selfies"

Your AR model must be:

class Selfy < ApplicationRecord
end
class Like < ApplicationRecord
belongs_to :user
belongs_to :selfy
end
class User < ApplicationRecord
has_many :selfies
end

Rails Conventions pluralisations

This question is fairly old but I stumbled across it looking for a way to handle the word staff(as in employees at a company) correctly.

The reason it pluralises with the 's' is because the plural of staff(as in wizards staff) is staffs(staves is also valid). So if you needed a table with different kinds of (wizards)staffs it is valid. However the plural of staff(as in people at a company) is staff.

Rails assumes that you are referring to the wizards staff in this instance. With words with only one meaning that don't have a separate plural, this works correctly:

[1] pry(main)> "sheep".pluralize == "sheep" 
=> true

How to fix this?

config/initializers/inflections.rb

ActiveSupport::Inflector.inflections do |inflect|
inflect.plural "staff", "staff"
end

This will tell rails that the plural of staff is staff and the naming conventions will be respected.

rails generate scaffold staff name:string description:text active:boolean

      invoke  active_record
create db/migrate/20220518125452_create_staff.rb
create app/models/staff.rb
invoke test_unit
create test/unit/staff_test.rb
create test/fixtures/staff.yml
invoke resource_route
route resources :staff
invoke inherited_resources_controller
create app/controllers/staff_controller.rb
invoke erb
create app/views/staff
create app/views/staff/index.html.erb
create app/views/staff/edit.html.erb
create app/views/staff/show.html.erb
create app/views/staff/new.html.erb
create app/views/staff/_form.html.erb
invoke test_unit
create test/functional/staff_controller_test.rb
invoke helper
create app/helpers/staff_helper.rb
invoke test_unit
create test/unit/helpers/staff_helper_test.rb
invoke assets
invoke coffee
create app/assets/javascripts/staff.js.coffee
invoke scss
create app/assets/stylesheets/staff.css.scss
invoke scss
identical app/assets/stylesheets/scaffolds.css.scss

The migration:

create_table :staff do |t|
t.string :name
t.text :description
t.boolean :active

t.timestamps
end

Q about Ruby on Rails Naming Conventions

I believe some aspects of Rails will let you do what you wish (e.g., you can have an attribute start with an upper case letter rather than the generally assumed lower case), but it will be a lot more work for you if you deviate from their conventions. A lot of Rails framework functionality assumes the Rails convention and you might lose the ability to use some of those facilities. And then there are some I'm not sure there's a way to work around, like the naming of a file for a model (client_info.rb) versus the class inside (ClientInfo). Those are assumed to align.

My recommendation would be to learn the Rails way unless you want to live in your own Rails convention world, isolated from the rest of the Rails world. It bugged me too, at first, but I got used to it pretty quickly. :)

Action View naming conventions for templates in Rails 4

It is derived from method naming convention. Since a view is rendered in response to an action, it shares action's name. Since action is a method, and...

  • Use snake_case for symbols, methods and variables. [source, Ruby Style Guide]

You should write multi-word action names like_this.



Related Topics



Leave a reply



Submit