:scope (validates_uniqueness) question

Consider the following table:
±—±-------±-------±----------+
| id | seq_no | request| response |
±—±-------±-------±----------+
| 42 | 1 | req1 | 0106 |
| 43 | 2 | req2 | 2343 |
| 44 | 3 | test | OK |
±—±-------±-------±----------+

I in the model that wraps this table, i have defined a constraint that
validates the uniqueness of the response for each request type. I.e.
validates_uniqueness_of :response, :scope => :request, :if => :response?

However, I not want to modify this constraint so that the uniqueness is
only validated for requests re1 and req2. i.e. I want to be able to
create the following table.
±—±-------±-------±----------+
| id | seq_no | request| response |
±—±-------±-------±----------+
| 42 | 1 | req1 | 0106 |
| 43 | 2 | req2 | 2343 |
| 44 | 3 | test | OK |
| 45 | 4 | test | OK |
±—±-------±-------±----------+

What should my validates_uniqueness_of statement look like?

Tim C. wrote:

Consider the following table:
±—±-------±-------±----------+
| id | seq_no | request| response |
±—±-------±-------±----------+
| 42 | 1 | req1 | 0106 |
| 43 | 2 | req2 | 2343 |
| 44 | 3 | test | OK |
±—±-------±-------±----------+

I in the model that wraps this table, i have defined a constraint that
validates the uniqueness of the response for each request type. I.e.
validates_uniqueness_of :response, :scope => :request, :if => :response?

However, I not want to modify this constraint so that the uniqueness is
only validated for requests re1 and req2. i.e. I want to be able to
create the following table.
±—±-------±-------±----------+
| id | seq_no | request| response |
±—±-------±-------±----------+
| 42 | 1 | req1 | 0106 |
| 43 | 2 | req2 | 2343 |
| 44 | 3 | test | OK |
| 45 | 4 | test | OK |
±—±-------±-------±----------+

What should my validates_uniqueness_of statement look like?

Hi Tim,
May be this will help

validates_uniqueness_of :response, :scope => :request, :if =>
ModelName.new{|model_names| model_names.request != “test” }

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs