Welcome to Working With Rails


Discussion Forums

Discuss all things Ruby on Rails with perhaps the web's most vibrant group of Ruby on Rails enthusiasts.
Validation question
4 Posts
Validation question


could someone help me with a validation question i have ?

I am importing data from a .csv-file into my database via FasterCSV (http://fastercsv.rubyforge.org/). Now, while reading the data into my database, the user will be able to assign "month" and "year" to the data.

The migration looks like this:

create_table :imodhwdbs, :force => true do |t|

t.column :kompid,          :string,   :limit => 8
t.column :assign_month,    :string,   :limit => 8
t.column :assign_year,     :string,   :limit => 4

How would i write a validation to check for the combination :kompid, :assign_month and :assign_year to be unique in the database. If there is another combination of those entries already in place, i could throw an error, but how do i check the uniqueness of this combination ?


If i am not wrong you can use the :scope option in your validation


validates_uniqueness_of :kompid,

                        :scope    => :assign_month,<br />
                        :message  => "^XXXXXXXX"
Forum Rating: 100
Sign In To Rate Post

Yes i think Balaji is correct. I'll expand on that an say it should be something like this:

validates_uniqueness_of :kompid, :assign_month, :assign_year :scope => [:kompid, :assign_month, :assign_year], :message => “XXXXXXXX”

That might do what you want. I hope :)

Thank you for your reply Balaji and Raul. I will try that out and let you know, if it worked for my problem.

4 Posts
Login to add your message