Ruby on Rails: How do you explicitly define plural names and singular names in Rails?
In config/initializers, you will find a file called inflections.rb. There are some instructions in here, but you will want something along the lines of:
ActiveSupport::Inflector.inflections do |inflect|
inflect.irregular 'bonus', 'bonuses'
end
Ruby on Rails - Define plural name of Model or Table
You can do something like this in your config/initializers/inflections.rb
file.
ActiveSupport::Inflector.inflections do |inflect|
inflect.irregular 'interesse', 'interesses'
end
Is there a plural/singular reference list for Rails?
You've pretty much got it.
Think of a model as controlling a single thing, so it's singular, and a controller controls a group of things, so it's plural. Scaffolds center around a model, so that's singular just like the model.
Views and helpers are related to the controller, so they're plural. Migrations don't care.
When in doubt, pass --pretend
to the generator and see what it will do.
Intelligent plural always intelligent?
Rails knows a lot of plurals. It can handle "city," for example:
1.9.2p318 :001 > "city".pluralize
=> "cities"
1.9.2p318 :002 > "cities".singular
=> "city"
However, you may find plurals it doesn't know, and won't be learning. See the documentation for ActiveSupport::Inflector
The Rails core team has stated patches for the inflections library
will not be accepted in order to avoid breaking legacy applications
which may be relying on errant inflections. If you discover an
incorrect inflection and require it for your application, you’ll need
to correct it yourself (explained below).
How do you correct it yourself? In config/initializers/inflections.rb
. For example:
ActiveSupport::Inflector.inflections do |inflect|
inflect.plural /^(.*)(l)ens$/i, '\1\2enses'
end
See again the docs for ActiveSupport::Inflector for more on how to teach rails new inflections.
Rails using plural table names even though I told it to use singular
My guess is that you've named your fixture accounts.yml
or used the directive fixtures :accounts
in one of the performance test. Rails fill the related table using the fixture name without knowledge about the model.
Rails ActiveRails joins - table name in plural/singular
The answer to when to use singular or plural lies in the name of the association. Usually has_many
associations are named in plural, whereas belongs_to
associations are named in singular. Behind the scenes when building the query ActiveRecord will make sure to always use the right table name (plural). In Rails there is the concept of "convention over configuration", which means a lot of stuff have a predefined approach that will work out of the box. E.g. take an association belongs_to :product
. ActiveRecord will look for a column product_id
in the table products
and use it to load the product. Another example, has_many :products
- ActiveRecord will look for a table called products
and assume it has a column your_model_id
. Thus it will load all the products for you model instance.
Back to your question, when to use singular or plural. If you are not sure, check your association definition in the respective model. In general, has_many
associations use plural, belongs_to
associations use singular. Here is an example with two models:
class Employee
has_many :tasks
end
class Task
belongs_to :employee
end
And here are some joins
examples:
Employee.joins(:tasks) # plural due to has_many
Task.joins(:employee) # singular due to belongs_to
Hope it was helpful!
Ruby on Rails Automatic Variable Name (Plural vs Singular)
Undefined method error
The issue with the undefined method probably has to do with the call being a plural call (quiz_bfs
), which expects that you have a has_many association, while your model only defines a has_one association:
has_one :quiz_bs
has_one :quiz_bf
Either a) the use of quiz_bfs
is a typo and should be quiz_bf
, b) the has_one is incorrect, or c) you don't need the call to current_user.quiz_bfs
at all to assign a value to @quiz_bf
. It's hard to tell without knowing more details, but it looks like you can just remove current_user.quiz_bfs
.
Bonus on routes
While it wasn't this question, specifically, you mentioned the pain of a resource ending in 's' being treated as a plural. If you haven't seen it yet, you can have your quiz_bs
routes named appropriately. To do so, you can use this form in your config/routes.rb
file:
resources :quiz_bs, as: :quiz_bs
This should give you the route helper names that you would like. You can read more about that in the Overriding Named Helpers section of Rails Routing from the Outside In.
You may want to name your controller QuizBsController
, as well, and you can use the controller:
override on the resources route definition to do that. Try this and see if it gives you the right controller:
resources :quiz_bs, controller: 'quiz_bs'
You can always combine both route methods and use something like this:
resources :quiz_bs, controller: 'quiz_bs', as: :quiz_bs
Check out the Specifying a Controller to Use section of Rails Routing from the Outside In for more information and usage details.
Rails path helper with singular controller names
- name for
fish#index
isfish_index
- name for
fish#show
isfish
So you should use:
<%= link_to 'My fishes', fish_index_path %>
And link fish's profile to:
<%= link_to 'Biggest Fish', fish_path(@fish) %>
Clear way to handle singular and plural case of Models and Symbols in Rails modules
See http://apidock.com/rails/String. There's a lot functions which might help you solve your problem. For instance, String#pluralize
and String#pluralize
might help with plural. After generating the string which you need you can convert it to symbol using String#to_sym
.
Related Topics
Ruby Read CSV File as Utf-8 And/Or Convert Ascii-8Bit Encoding to Utf-8
Finding the Element of a Ruby Array with the Maximum Value for a Particular Attribute
(Ruby) Getting Net::Smtp Working with Gmail...
What's the Difference Between Colon ":" and Fat Arrow "=>"
Missing Symbol When Installing Ruby-2.3.0 on Os X 10.11.6 by Rvm
Ubuntu Rails Install Fails on Zlib
How to Get Readline Support in Irb Using Rvm on Ubuntu 11.10
Why Does Ruby Have Trueclass and Falseclass Instead of a Single Boolean Class
Is There Equivalent for PHP's Print_R in Ruby/Rails
How to Check to See If My Array Includes an Object
Rails - Sort by Join Table Data
Change the Binding of a Proc in Ruby
How to Install JSON Gem - Failed to Build Gem Native Extension(MAC 10.10)
Why Isn't 'Method=' Treated the Same as Any Other Method
How to Get Sinatra to Auto-Reload the File After Each Change