Multi - Field Uniqueness

25 views
Skip to first unread message

Arthur Baldwin

unread,
Jan 16, 2012, 2:44:18 PM1/16/12
to Hobo Users
I'm going through the Rapid Rails 3 beta 6 book and doing the "my-first-app" example.  Everything is working fine.
But what if I want to guarantee the uniqueness of a customer/contact based upon a combination of the following fields:

Name
Address 1
City
State
ZipCode

In other words, I could enter more than one "John Smith" if at least one of those fields was different.  How can I get this to work?

Sincerely,

Arthur Baldwin

kevinpfromnm

unread,
Jan 16, 2012, 3:03:26 PM1/16/12
to hobo...@googlegroups.com, Arthur Baldwin
you'll need to fall back to the rails validation (which the hobo is a rails validation, just a shortcut for a common use case).

IIRC, the syntax is like validates_uniqueness_of :field, :scope => [:other_field1,...]

If that's not enough to get you going, you can get to the rails docs pretty easy with a quick google search on validates_uniquess_of (should autocomplete after the u)

Arthur Baldwin

unread,
Jan 16, 2012, 3:24:25 PM1/16/12
to hobo...@googlegroups.com
What do you mean when you say "fall back to the rails validation"?  Do I need to make changes to a different file than the conact.rb file in the models directory?

Arthur


From: kevinpfromnm <kevinp...@gmail.com>
To: hobo...@googlegroups.com
Cc: Arthur Baldwin <eeng...@yahoo.com>
Sent: Monday, January 16, 2012 12:03 PMco
Subject: [Hobo Users] Re: Multi - Field Uniqueness

you'll need to fall back to the rails validation (which the hobo is a rails validation, just a shortcut for a common use case).

IIRC, the syntax is like validates_uniqueness_of :field, :scope => [:other_field1,...]

If that's not enough to get you going, you can get to the rails docs pretty easy with a quick google search on validates_uniquess_of (should autocomplete after the u)
--
You received this message because you are subscribed to the Google Groups "Hobo Users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/hobousers/-/VvjxOt3xpyAJ.
To post to this group, send email to hobo...@googlegroups.com.
To unsubscribe from this group, send email to hobousers+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/hobousers?hl=en.


Arthur Baldwin

unread,
Jan 16, 2012, 4:11:55 PM1/16/12
to hobo...@googlegroups.com
Ok, I got it working!  I had forgotten to include a colon in front of the field names between the opening and closing brackets.

Arthur


From: Arthur Baldwin <eeng...@yahoo.com>
To: "hobo...@googlegroups.com" <hobo...@googlegroups.com>
Sent: Monday, January 16, 2012 12:24 PM
Subject: Re: [Hobo Users] Re: Multi - Field Uniqueness

Owen Dall

unread,
Jan 16, 2012, 5:33:26 PM1/16/12
to hobo...@googlegroups.com
Arthur (and others) , it would help others if you would post your working code after you get it working so it will show up when someone searches the group. : -)

-Owen
--
-Owen
 
Owen Dall, Chief Systems Architect
Barquin International

Arthur Baldwin

unread,
Jan 16, 2012, 9:58:42 PM1/16/12
to hobo...@googlegroups.com
The solution was to change the contents of contact.rb to the following:

class Contact < ActiveRecord::Base

  hobo_model # Don't put anything above this

  fields do
    name    :string, :required
    company :string
    address_1 :string
    address_2 :string
    city      :string
    state     :string
    date_met  :date
    married   :boolean
    age       :integer
    notes     :text
    timestamps
  end

validates_uniqueness_of :name, :scope => [:address_1, :city, :state]

  # --- Permissions --- #

  def create_permitted?
    acting_user.administrator?
  end

  def update_permitted?
    acting_user.administrator?
  end

  def destroy_permitted?
    acting_user.administrator?
  end

  def view_permitted?(field)
    true
  end

end

I did not take the time to add the ZipCode field or address_2, but you get the idea.  The scope adds more fields to the name field to determine whether the record has enough "uniqueness".  For any company with a large number of customers nation wide, this sort of thing is required.  Works exactly like I had hoped.  I tried entering "John Smith" for two separate records, with slightly different addresses, and it saved both records.  Now I want to find a way to visually distinguish easily between those two records from the first "Contact" page.  In other words, create a calculated field that concatenates the name field, address_1, city, state, and ZipCode so that I know immediately which "John Smith" to click on.

Sincerely,

Arthur Baldwin
Sent: Monday, January 16, 2012 1:11 PM

JezC

unread,
Jan 17, 2012, 1:57:29 AM1/17/12
to Hobo Users
Ooh - I'm pretty new to hobo, but I just solved this problem (visual
identification by uniquely naming something where no individual field
is unique)...

There is a "name" attribute for fields, so you can pick one field as
the name to be used (there's also rules for picking fields called
"name", "title", etc, by default):

fields do
first, :string, :name => true
second, :string
...

You can't pick two or more fields like this - you get only one field
treated as the name. Try this instead:

def name
"#{name}, #{address1}, #{city}, #{state}"
end

The method "name" will be used to calculate the value to show in
Forms, Cards and Pages. Works a treat for me. :)

I think this is the first answer I've given on this list, rather than
yet another newbie question. Feel my smugnitude (yes, I think this is
actually the right answer).

Cheers, JeremyC.
>  From: Arthur Baldwin <eengn...@yahoo.com>
> To: "hobo...@googlegroups.com" <hobo...@googlegroups.com>
> Sent: Monday, January 16, 2012 1:11 PM
> Subject: Re: [Hobo Users] Re: Multi - Field Uniqueness
>
> Ok, I got it working!  I had forgotten to include a colon in front of the field names between the opening and closing brackets.
>
> Arthur
>
> ________________________________
>  From: Arthur Baldwin <eengn...@yahoo.com>
> To: "hobo...@googlegroups.com" <hobo...@googlegroups.com>
> Sent: Monday, January 16, 2012 12:24 PM
> Subject: Re: [Hobo Users] Re: Multi - Field Uniqueness
>
> What do you mean when you say "fall back to the rails validation"?  Do I need to make changes to a different file than the conact.rb file in the models directory?
>
> Arthur
>
> ________________________________
>  From: kevinpfromnm <kevinpfro...@gmail.com>
> To: hobo...@googlegroups.com
> Cc: Arthur Baldwin <eengn...@yahoo.com>
> Sent: Monday, January 16, 2012 12:03 PMco
> Subject: [Hobo Users] Re: Multi - Field Uniqueness
>
> you'll need to fall back to the rails validation (which the hobo is a rails validation, just a shortcut for a common use case).
>
> IIRC, the syntax is like validates_uniqueness_of :field, :scope => [:other_field1,...]
>
> If that's not enough to get you going, you can get to the rails docs pretty easy with a quick google search on validates_uniquess_of (should autocomplete after the u)
> --
> You received this message because you are subscribed to the Google Groups "Hobo Users" group.
> To view this discussion on the web visithttps://groups.google.com/d/msg/hobousers/-/VvjxOt3xpyAJ.
> To post to this group, send email to hobo...@googlegroups.com.
> To unsubscribe from this group, send email to hobousers+...@googlegroups.com.
> For more options, visit this group athttp://groups.google.com/group/hobousers?hl=en.
>
> --
> You received this message because you are subscribed to the Google Groups "Hobo Users" group.
> To post to this group, send email to hobo...@googlegroups.com.
> To unsubscribe from this group, send email to hobousers+...@googlegroups.com.
> For more options, visit this group athttp://groups.google.com/group/hobousers?hl=en.

Owen Dall

unread,
Jan 17, 2012, 10:08:14 PM1/17/12
to hobo...@googlegroups.com
Thanks for getting into the mix and building our "Knowledge Space"

For more options, visit this group at http://groups.google.com/group/hobousers?hl=en.

Arthur Baldwin

unread,
Jan 17, 2012, 10:31:07 PM1/17/12
to hobo...@googlegroups.com
Dear Jeremy,

I tried your idea by putting the following code into my contact.rb file:

class Contact < ActiveRecord::Base

  hobo_model # Don't put anything above this

  fields do
    # name       :string

    company    :string
    address_1  :string
    address_2  :string
    city       :string
    state      :string
    postalcode :string
    birthdate  :date

    married    :boolean
    age        :integer
    notes      :text
    timestamps
  end
 
  # validates_uniqueness_of :name, :scope => [:address_1, :address_2, :city, :state, :postalcode]

  def name
    "#{name}, #{address_1}, #{city}, #{state}, #{postalcode}"
  end

 
  # --- Permissions --- #

  def create_permitted?
    acting_user.administrator?
  end

  def update_permitted?
    acting_user.administrator?
  end

  def destroy_permitted?
    acting_user.administrator?
  end

  def view_permitted?(field)
    true
  end

end
but I get a crash as soon as I click on the Contacts tab.  You said it worked for you, so I'm sure I don't know about something else.

Sincerely,

Arthur Baldwin

From: Owen Dall <od...@barquin.com>
To: hobo...@googlegroups.com
Sent: Tuesday, January 17, 2012 7:08 PM

JezC

unread,
Jan 18, 2012, 1:51:00 AM1/18/12
to Hobo Users
Hi,

I suspect two possible reasons for a crash:

* using "#{name}" inside a definition of name is pretty certainly
infinitely recursive - "name" will call itself again and again until
the system runs out of memory
* name is now a commented out field - so there is no field to
substitute

Try changing to

  def name
    "#{company}, #{address_1}, #{city}, #{state}, #{postalcode}"
  end

More likely to work. :)
> >For more options, visit this group athttp://groups.google.com/group/hobousers?hl=en.
Reply all
Reply to author
Forward
0 new messages