Validates_uniqueness_of two fields

Is it possible to do a validates_uniqueness_of :name, :zip but only in
that context? I’d like to be able to have that zip used again, but not
in conjunction with a name already with that zip.

Does that make sense?

Guest wrote:

Is it possible to do a validates_uniqueness_of :name, :zip but only in
that context? I’d like to be able to have that zip used again, but not
in conjunction with a name already with that zip.

Does that make sense?

I’m new to Rails, but I think validate_uniqueness_of :name, :scope=>:zip
should do what you want.

Perfect. Thanks yo.

Eric N. wrote:

Guest wrote:

Is it possible to do a validates_uniqueness_of :name, :zip but only in
that context? I’d like to be able to have that zip used again, but not
in conjunction with a name already with that zip.

Does that make sense?

I’m new to Rails, but I think validate_uniqueness_of :name, :scope=>:zip
should do what you want.

Guest wrote:

Is it possible to do a validates_uniqueness_of :name, :zip but only in
that context? I’d like to be able to have that zip used again, but not
in conjunction with a name already with that zip.

Does that make sense?

Do you mean that the zip needs to be unique for each name (i.e. each
name can have only one of each zip), or the other way round. If it’s the
former, try:
validates_uniqueness of :zip, :scope => :name