jeudi 7 avril 2016

Rails validate model attribute based on value of another attribute

I have a rails app with User model which has state and city attributes.
I am using a gem to get the list of cities of a given state and country.
I want that while signing up if the city selected by user is not in the list of cities for the state selected by user then dont allow signup and show error.

  validates :state, presence: true, inclusion: { in: CS.states(:in).keys.collect{|x| x.to_s } }
  validates :city, presence: true, inclusion: { in: CS.cities((:state).to_sym, :in) }

I wrote this code in user.rb model but its not working.
:state should refer to the value submitted by user for 'state' attribute but i don't think that's working here. I want to pass the value of state given by user to the function CS.cities(). How do I fix this?

Aucun commentaire:

Enregistrer un commentaire