Consider you have two tables products and Oraganisation, where product belongs to a particular organisation.
Product Organisation
- id - id
- name - name
- organisation_id - description
So when you want to get the organisation name, which the product belongs to then you say,
But according to “Rails Anti-patterns” more than one dot notation in a statement is an example of anti pattern.
Normal fix
So How we can fix it, In Product model write a instance method organisation_name
just like below.
Using delegate
Writing methods for all the properties is a tedious task, Instead of that we can use delegate in rails.
You can simply use below code instead of writing a method organisation_name
.
Now you can use @product.organisation_name
to instead of @product.organisation.name
.
You can pass method name and target model/object via :to
option. Here :prefix
option determine whether the method name should prepend with object name or not.
If you wanna custom prefix, then
Now you can use as @product.org_name