On 11-11-19 03:15 PM, Matt Jones wrote:
On Nov 19, 2011, at 2:19 PM, Donald R. Ziesig wrote:

Hi All!

I thought I saw the solution to this problem a long time ago, but I can't seem 
to find it now.

I have a table of Properties with belongs_to Agents.  I want to restrict the 
uniqueness of the property's name only to a specific Agent such that Agent1 had 
Properties A, B and C and Agent2 adds a property B - right now that is rejected 
as being a duplicate, but it shouldn't fail.  I can do this with a bit of 
hacking, but there should be an easy way to do it.

in the Agents model:

   fields do
     name :string, :unique, :required
     *
     *
   end

   has_many :properties

in the Properties model:

   fields do
     name :string, :unique, :required  #I should be able to add something here 
I think.
     *
     *
    end

belongs to :agent

What am I missing here?
The :unique option in the field declaration is a shortcut - it doesn't accept 
all the options that the underlying Rails validation does. If you remove it and 
write out the full validation (on Property):

validates_uniqueness_of :name, :scope =>  :agent_id

BTW, the new Rails 3 (Hobo 1.3) way of doing this is:

   validates :name, :uniqueness => {:scope => :agent_id}

The advantage of this format it that you can put all the validations for a field into the same directive.

Cheers,
Henry

it should do exactly what you're describing.

--Matt Jones


--
Henry Baragar
Instantiated Software
416-907-8454 x42

--
You received this message because you are subscribed to the Google Groups "Hobo 
Users" 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/hobousers?hl=en.

Reply via email to