Vyacheslav Loginov
Vyacheslav Loginov

Reputation: 3216

activerecord belongs_to refactoring

I have many belongs_to relations

belongs_to :breed
belongs_to :category
belongs_to :color
belongs_to :country
belongs_to :city
belongs_to :user

I collapse it to

[:breed, :category, :color, :country, :city, :user].each { |r| belongs_to r }

The question is: Is it possible more collapse like

[:breed, :category, :color, :country, :city, :user].belongs_to

and how compose such code

or something like

[:breed, :category, :color, :country, :city, :user].each &:belongs_to

or something more elegant

Upvotes: 0

Views: 61

Answers (1)

mdemolin
mdemolin

Reputation: 2534

You can still improve your solution with an array of symbols. But beside the fact I understand the will for elegancy and brevity, note that such a solution will prevent you from passing any option to your belongs_to... And there is no code smell in having multiple belongs_to

Upvotes: 2

Related Questions