On 23 February 2010 01:30, Rick DeNatale <[email protected]> wrote: > To create an index you can create a migration which includes a statement like > > add_index :widgets, [:a, :b], :unique => true) > > Assuming that the table name for the model is 'widgets' > > Now if you do this, you also need to be aware that if you try to save > a record with a uniqueness conflict the save will raise an > ActiveRecord::StatementInvalid exception, which you'll need to rescue > and do something like telling the user of the conflict so that he can > resolve it.
If you add a column called "combined_params" or similar, you can add a before_validate method to concatenate all of your parameters and write it to this column, this will let you do AR "validates_uniqueness_of" checks too. It would actually remove the need to have the key added across the columns in the DB, but I'd leave that as a safety net, as it would be too easy to accidentally (or deliberately) change a row's "combined_params" column, and risk duplicates. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

