All threads / How to validate model attribute

Ask A Question

Notifications

You’re not receiving notifications from this thread.

How to validate model attribute

Harris Mariano asked in Rails

Hello everyone, question.

I have the following code:

class Country < ActiveRecord::Base
has_many :cities, dependent: :destroy
accepts_nested_attributes_for :cities
end

class City < ActiveRecord::Base
belongs_to :country
has_many :islands, dependent: :destroy
end

class Island < ActiveRecord::Base
belongs_to :city
validates :name , uniqueness: { scope: :city_id }
end

How to make island name unique within the city and within the country. Thank you in advance.

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 48,387+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2021 GoRails, LLC. All rights reserved.