I looked around and it seems that to set pluralization rules for model names you put the following in your locale file
# es.yml
es:
activerecord:
models:
business:
one: Lugar
other: Lugares
However, when the model name is translated in plural, I still get Lugars instead of Lugares
Not sure what's wrong
It should work with Business.model_name.human(:count => 2)
Your problem is that you are mixing up two forms of pluralization of strings in Rails. The first is meant for internal purposes: for naming classes, variables, methods, table names, etc. This is pluralize, and to make it properly handle exceptions etc. you can define inflections in config/initializers/inflections.rb.
But this type of pluralization is not appropriate for translations. For that, you should use Business.model_name.human(:count => 2) (as #doesterr suggested), which will reference the locale file for the locale you are in, which is what you want.
For details see this answer.
Related
I want to create a model called Match. Will the table for Match be matches or matchs i.e pluralizing with 'es' instead of the normal 's'? If I have another table, Player, belonging to Match, should the reference be match_id? If rails understands how to plularize Match, what other unusual pluralizations does it accept e.g. a model called Misery which would have a normal pluralized form of miseries?
Based on meagar's comment, most english pluralizations are done correctly in rails. If in doubt, you can just open a rails console and type 'model_name".pluralize e.g.
'match'.pluralize
which returns 'matches'. If this doesn't work, then you can roll your own according to this SO answer.
much to quite a few people's disappointment. I've decided to learn Ruby on Rails (I've been told by many people it's pointless to learn it, but ruby seems easy to get into and rails is a fun framework).
So I'm just now slowly starting to wrap my head around the naming convention with singular being used for the model and plural being used for the controller.
One thing I'm having a bit of trouble finding concise information on is Capitalization vs lowercase usage.
Example being
#order = Order.create(order_date: Time.now, customer_id: #customer.id)
Why exactly is 'Order.create' capitalized but not 'order_date'?
When to invoke capitalization has really got me confused when I use the rails console. I don't know when I'm suppose to be capitalizing or why. I want to say it too has to do with the model/controller naming scheme but neither of them are plural so that can't be the answer right?
Thanks for any help that is given, I really would/do appreciate it.
CamelCase is used for classes and modules (e.g. String, Array, etc.)
snake_case is used for variables and methods (symbols are also usually snake_case)
SCREAMING_SNAKE_CASE is used for naming constants (e.g. STDOUT)
This is to do with naming conventions in Ruby and/or Rails.
Typically:
Create classes, modules using CamelCase.
Create methods, variables using underscore_case.
Create constants using UPPER_UNDERSCORE_CASE.
Take a look at different methods available in ActiveSupport to convert a string into various cases. http://apidock.com/rails/v4.1.8/String/camelize
Read Naming and Schema Conventions at Rails Guide.
A more succinct information.
HTH
This is more a ruby thing actually. In ruby you use capitalization (camel case) for class and module names. And snake_case for method and variable names.
In your specific case Order is a class, create is a method, #order is an instance variable. order_date and customer_id are symbols that represent the columns on the database.
Tweet to tweets is no big deal.
And I assume Country would become countries without any fuss. I'd even credit them with Cactus becoming cacti (correct me if I'm wrong).
But what if I have class Barnabus? Do I call its table barnabuss, barnabi, or barnabusses?
What about zxzzy? How are non-english words handled?
Are there any non-messy options for overriding the decapitalization and pluralization?
I'm new to Rails and the modified table names have me a bit off-put.
Links to relevant documentation are appreciated. This is the only documentation I have seen relevant to this, and its not helpful for understanding odd exceptions for table names.
Forgive me if this is already covered, the results I got were not exactly what I'm trying to figure out.
String#pluralize uses a heuristic with the rather simple general rules for the English language as a base and adding some known common exceptions.
Using ActiveSupport::Inflector you can customize its behavior.
In Rails projects you can find the inflections config at config/initializers/inflections.rb.
If you'd like to create a Barnabus model with a corresponding barnabi table then all you have to do is uncomment some inflections.rb content and explicitly tell Rails how to treat this word.
ActiveSupport::Inflector.inflections do |inflect|
inflect.irregular 'barnabus', 'barnabi'
end
P.S. Thanks to Holger Just for the edit.
You can trust it for most of the cases but if you have some doubtful model name with awkward plural names, you can always specify the table name in Model. e.g.
class SomeName < ActiveRecord::Base
self.table_name = 'any_name'
end
If pluralization in views/error messages are your concern, in the situation that Rails fails magnificently with pluralizing a tricky table name you can add some localization to your app and tell it what you want it to use under various contexts. See http://guides.rubyonrails.org/i18n.html#translations-for-active-record-models
Here's an example of pluralization rules in action:
activerecord:
models:
barnabus:
zero: Barnabi
one: Barnabus
few: Barnabi
other: Barnabi
How should I name a action if it contains 2 words:
word_other
wordOther
wordother
?
I assume you're talking about controller actions? In that case, they need to be underscored: get_user.
The convention is for variable and method names to be underscored: #new_instance.get_user. This ensures that controller methods map cleanly to actions. It also improves readability and helps distinguish method_names from ClassNames
In Ruby, method names have the convention that they should be separated by underscores, so Rails action names are no different.
It should be with underscore: word_other.
Rails action name is actually just a method name and Ruby's convention for method name is in lower case with words separated by underscore.
I was trying to find an official statement about this, but unfortunately I couldn't find it. But you can have a look at the methods available in Ruby Core to see how Ruby methods are named. There's also a write up on the naming convention for both Ruby and Rails.
With Rails, naming convention is even more important because one of the philosophy of Rails is convention over configuration. For example, if your controller class name is BookShopsController, you need to use book_shops to refer to it in your routes.
resources :book_shops
# or
match 'book_shops/:id' => 'book_shops#show'
If you don't follow along with the convention, you may find that things are more difficult to work with or it may not work as expected.
You should name it word_other.
In short:
methods and variables: all_lower_case_seperated_by_underscores
class- and module-names: CamelCased
constants: UPPERCASE
This article is very good summary of the ruby and rails naming conventions.
In general, ruby identifiers should use snake_case rather than camelCase. However, Rails action names should be selected from a much smaller list: show, index, new, create, edit, update, destroy. That is to say that following RESTful conventions will lead you to a simpler system that more accurately reflects the semantics of the web and allows you to more cleanly model your domain's resources and should be preferred to other action naming 'systems'.
In rails application I have two models: Food and Drink.
Both food and drink have a name, which has to be stored in two languages.
How do I better realize translations for theese tables?
First solution I realized was to replace name column with name_en and name_ru.
Another solution is to encode with YAML hash like { :en => 'eng', :ru => 'rus' } and store yaml as a name.
What would you recommend, assuming content is not static?
Maybe there's good article?
The first option (name_en, name_ru) is the easiest to implement.
You could include a name method that returns the right value depending on the selected locale. You could even create a module, if you are going to use this on lots of models/fields:
class Food < ActiveRecord::Base
...
def name
self.send("name_#{I18n.locale}")
end
end
If in the future you must include an additional language, you will have to add a migration, of course. But that shoudn't be too troublesome.
The second one (encoding using YAML) seems a bit more cumbersome - you will not have to make an additional migration with it, but you loose other functionality. For example, search is made much more difficult - you can't use SQL any more for looking through descriptions, as they are coded on YAML instead of being plain text.
So I recommend having two fields.