I have an address table in rails:
class Address < ApplicationRecord
validates :street, :city
presence: { allow_blank: false }
end
the table is associated with several tables and I added validation to prevent street and city from being null or "", but I wanted this validation to only run on the associated table user
class User < ApplicationRecord
belongs_to :address
validates_associated :address
end
User has a belongs_to for address, and would not like to put a belongs_to inside the address. How do I make address validations only work when the table is user?
When you only want to validate the presence of
streetandcitywhen there is auseror at least one user associated with theaddressthen I would add aifcondition to the validate definition, like this: