Posted over 6 years ago. Visible to the public. Repeats.

Always, always declare your associations with symbols

Never ever declare your associations with a string, especially when doing metaprogramming. A common mistake is something like

Copy
# WRONG class Page < ActiveRecord::Base %w[main sub].each do |type| belongs_to "#{type}_title" end end # RIGHT class Page < ActiveRecord::Base %w[main sub].each do |type| belongs_to "#{type}_title".to_sym end end

Always append a to_sym, otherwise you'll have all kinds of fun.

By refactoring problematic code and creating automated tests, makandra can vastly improve the maintainability of your Rails application.

Owner of this card:

Avatar
Tobias Kraze
Last edit:
6 months ago
by Besprechungs-PC
Keywords:
belongs_to, has_one, has_many
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Tobias Kraze to makandra dev
This website uses cookies to improve usability and analyze traffic.
Accept or learn more