Majiy
Majiy

Reputation: 1920

Rails: validates uniqueness through

I have the following Models:

Language

Itemtype

Item
    belongs_to :itemtype

LocalisedItem
    belongs_to :item
    belongs_to :language

The LocalisedItem model has an attribute called "title".

I want to validate the uniqueness of said "title" attribute. My problem is the scope: It´s supposed to be unique per language (easy) and itemtype, which I could not figure out how to do until now.

My best try...

validates :title, :uniqueness => { :scope => [:language_id, 'item.itemtype_id'] }

...fails with "NoMethodError: undefined method `item.itemtype_id'".

Is there any way to check for uniqueness in the way described?

Upvotes: 1

Views: 336

Answers (1)

Reda.benh
Reda.benh

Reputation: 31

You can use this format for validate uniqueness with a scope:

validates_uniqueness_of :title, :scope => :language_id

Upvotes: 1

Related Questions