def validate_time_zone
if time_zone.present? && org.present? && !org.country.states.map(&:time_zone).uniq.include?(time_zone)
errors.add(:time_zone, :invalid)
end
end
I have a user model with the following relations:
- User can optionally belong to Org
- Org belongs to a Country
- A Country has some timezones (not unique as two states may have the same timezone).
So I validate that if the user is associated with an organization then it must have a valid timezone. Valid timezone means any time zone that comes under organization country. Ex Organization is ABC, Country is USA and Timezones are CET, Pacific, and Other US Time Zone. Then the user must use a timezone within the above three only.
I have filtered the condition but I am not sure breaking down will improve quality or further performance.
Can we used scope here too in country or organization or user module as it help in DRY not just validation.
Country
at least two times, just to be followed by organization county: is the latter a typing error? \$\endgroup\$