has_one working?

40 views
Skip to first unread message

Bob Sleys

unread,
Oct 28, 2011, 10:07:31 AM10/28/11
to hobo...@googlegroups.com
Using Hobo RC3 and have the following model

class Organization < ActiveRecord::Base

  hobo_model # Don't put anything above this

  fields do
    name :string, :required, :unique
    facility  :string
    address :text
    city  :string
    state :string
    zip   :string
    phone :string
    timestamps
  end

  has_many :users, :accessible => :true

  has_one :primary_contact, :through => :users

  children :users
...

end

and I have   belongs_to :organization, :accessible => :true in the users.rb model

However the hobo g migration doesn't pick up the requirement for a primary_contact_id field in the table nor does the form try to create an input field for it.  I've tried adding the primary_contact_id to the list of fields but the auto generated form still doesn't create an input filed for it.  I know I could set it up all myself but I'm just wondering why Hobo isn't doing it automatically like a has_many

Bob

Owen Dall

unread,
Oct 28, 2011, 10:09:18 AM10/28/11
to hobo...@googlegroups.com
I don't remember has_one being implemented in Hobo, and I don't remember why not. : -)

Perhaps Matt or Bryan can comment...

--
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/-/D5UF91irwdQJ.
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.



--
Owen Dall, Chief Systems Architect
Barquin International

Bob Sleys

unread,
Oct 28, 2011, 10:17:35 AM10/28/11
to hobo...@googlegroups.com
If it's not there I can do it manually it just seemed odd it wouldn't be there.  Unless I'm going about this wrong.  Is there some other way I would go about specifying something like a primary contact to an organization assuming they are one of users?

In this case an organization can have many users associated with it, standard has_many like and 1 of those users is the primary contact for the organization.  I know I'd have to tweak the selection list for the has_one at any rate to limit it to the users assocated with the organization otherwise I'd get a list of all users.

Bob

Matt Jones

unread,
Oct 28, 2011, 10:49:39 AM10/28/11
to hobo...@googlegroups.com

I'm not following what you're going for here - has_one doesn't create a foreign key, especially not a :through...

A more typical way to describe this would be either with belongs_to:

class Organization < ActiveRecord::Base
...
belongs_to :primary_contact, :class_name => 'User'
end

This will give you a select-one on the forms for organization - but it won't automatically scope the selection to the :users association, and will need some extra logic to handle the "primary contact manually deleted leaving invalid primary_contact_id" situation.

An alternative would be to define a flag on User:

class User < ActiveRecord::Base
fields do
...
primary_contact :boolean, :default => false
end
end

And then a has_one association with conditions:

class Organization < ActiveRecord::Base
...
has_one :primary_contact, :class_name => 'User', :conditions => { :primary_contact => true }
end

You'll need some custom views and/or model logic to ensure that you've only got one selected at any given time.

--Matt Jones

Bryan Larsen

unread,
Oct 28, 2011, 12:26:05 PM10/28/11
to hobo...@googlegroups.com
On Fri, Oct 28, 2011 at 10:09 AM, Owen Dall <od...@barquin.com> wrote:
> I don't remember has_one being implemented in Hobo, and I don't remember why
> not. : -)

The biggest reason is "because nobody implemented it", but the other
reason is that internally in Rails, has_one is basically a has_many
that's constrained to having 1 child, and has always been the ugly
stepchild. By now I'm sure most of the warts have been cleaned up
but certainly at some points in time it has had problems.

Bryan

Owen Dall

unread,
Oct 28, 2011, 12:46:05 PM10/28/11
to hobo...@googlegroups.com
Perhaps we can revisit for Hobo 1.4.   Seems to keep coming up...

-Owen

--
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 at http://groups.google.com/group/hobousers?hl=en.

Reply all
Reply to author
Forward
0 new messages