Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix numericality validator when defined on abstract class: #38193

Merged

Conversation

Edouard-chin
Copy link
Member

@Edouard-chin Edouard-chin commented Jan 9, 2020

Fix numericality validator when defined on abstract class:

  • Problem

    It's no longer possible to define a numeric validation on abstract
    class:

      class AnimalsBase < ApplicationRecord
        self.abstract_class = true
    
        validates :age, numericality: { min: 18 }
      end
    
      class Dog < AnimalsBase
      end
    
      Dog.create!(age: 0) => ActiveRecord::TableNotSpecified: AnimalsBase has no table configured. Set one with AnimalsBase.table_name=

    Solution

    Instead of trying to get the type for attribute on the class
    defining the validation, get it from the record being validated.

cc/ @rafaelfranca @casperisfine @gmcgibbon

@Edouard-chin Edouard-chin force-pushed the ec-fix-numericality-on-abstract-class branch from 027c344 to 40b9e8f Compare January 9, 2020 02:43
- ### Problem

  It's no longer possible to define a numeric validation on abstract
  class:

  ```ruby
    class AnimalsBase < ApplicationRecord
      self.abstract_class = true

      validates :age, numericality: { min: 18 }
    end

    class Dog < AnimalsBase
    end

    Dog.create!(age: 0) => ActiveRecord::TableNotSpecified: Dog has no table configured. Set one with Dog.table_name=
  ```

  ### Solution

  Instead of trying to get the type for attribute on the class
  defining the validation, get it from the record being validated.
@Edouard-chin Edouard-chin force-pushed the ec-fix-numericality-on-abstract-class branch from 40b9e8f to c507e93 Compare January 10, 2020 14:36
@rafaelfranca rafaelfranca merged commit 89a7219 into rails:master Jan 10, 2020
@Edouard-chin Edouard-chin deleted the ec-fix-numericality-on-abstract-class branch January 10, 2020 16:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants