STI rails redefine areas

Say I have an STI setup as follows:

class User < ActiveRecord::Base scope :busy, -> { where('busy_factor > 1') } end class HeroUser < User scope :busy, -> { where('busy_factor > 5') } end 

Thus, hero users have a different threshold for employment.

Now, if I do this, I get warnings:

 Creating scope :busy. Overwriting existing method HeroUser.busy. 

Everything seems to work correctly, but I wonder if there is a better way to do this.

+4
source share
1 answer

Cleaner way:

  • Delete region for child models
  • Introduce a class method (e.g. busy_factor) that returns a busy factor for this type of model.
  • If necessary, override class methods in descendants.
  • Repeat the scope in the base class as:

    area: busy, → {where ('busy_factor>?', self.busy_factor)}

Hope this helps.

+8
source

Source: https://habr.com/ru/post/1445143/


All Articles