On Oct 28, 2011, at 10:07 AM, Bob Sleys wrote:
> 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
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
--
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.