Reversing Foreign Key Relationship?

1 view
Skip to first unread message

Brent

unread,
Oct 11, 2010, 10:58:47 PM10/11/10
to rubyamf
I'm a situation where I have a person class that refers to an address
class. Typically the person class would have a has_one with address
and address would have a belongs_to with person. The problem is that
this puts a person_id in the address class. This is a problem since I
want to use addresses with other classes. I'd prefer person to have an
address_id. Is there any way to do this and have it work with rubyamf?

Brent

fosrias

unread,
Oct 11, 2010, 11:30:43 PM10/11/10
to rubyamf
I don't think rubyamf is the limitation, it is how you define the
association in Rails. You could, for example, set up a one-to-many
relationship (address being the one) and then on the person side it
all works as long as you constrain the system to not allow multiple
persons to point to the same address (unless that did not matter). In
any case, person would always belong to "address." I.e. have one
address. Not sure where you see rubyamf being the bottleneck.

Brent

unread,
Oct 11, 2010, 11:39:00 PM10/11/10
to rubyamf
Thanks for your response. I'm not suggesting that RubyAMF is the
problem. I'm a flex dev and ruby/rails newb. I was just having
issues with the has_one belongs_to situation and my use case. I'll
try what you're suggesting.

Brent

fosrias

unread,
Oct 12, 2010, 12:35:33 AM10/12/10
to rubyamf
Brent:

You might find the following helpful for getting up to speed with the
versatility of rails associations:

http://guides.rubyonrails.org/association_basics.html

Mark

On Oct 11, 8:39 pm, Brent <pub...@brentbonet.com> wrote:
> Thanks for your response. I'm not suggesting that RubyAMF is the
> problem.  I'm a flex dev and ruby/rails newb.  I was just having
> issues with the has_one belongs_to situation and my use case.  I'll
> try what you're suggesting.
>
> Brent
>
> On Oct 11, 9:30 pm, fosrias <fosteri...@gmail.com> wrote:
>
>
>
> > I don't think rubyamf is the limitation, it is how you define the
> > association in Rails. You could, for example, set up a one-to-many
> > relationship (address being the one) and then on the person side it
> > all works as long as you constrain the system to not allow multiple
> > persons to point to the same address (unless that did not matter). In
> > any case, person would always belong to "address." I.e. have one
> > address. Not sure where you see rubyamf being the bottleneck.
>
> > On Oct 11, 7:58 pm, Brent <pub...@brentbonet.com> wrote:
>
> > > I'm  a situation where I have a person class that refers to an address
> > > class.  Typically the person class would have a has_one with address
> > > and address would have a belongs_to with person. The problem is that
> > > this puts a person_id in the address class.  This is a problem since I
> > > want to use addresses with other classes. I'd prefer person to have an
> > > address_id. Is there any way to do this and have it work with rubyamf?
>
> > > Brent- Hide quoted text -
>
> - Show quoted text -

Brent

unread,
Oct 12, 2010, 12:43:16 AM10/12/10
to rubyamf
Thanks. Yes I'm familiar with it. In fact, it's open in a browser tab
right now. ;-)

Allen Wyma

unread,
Oct 12, 2010, 3:31:36 AM10/12/10
to rub...@googlegroups.com
Sorry, but I had to ask a question, Brent. If you're only planning on
having one address per person, why not put the address in the Person
table? If you plan on having multiple addresses, that would be when
you would want to have 2 different tables. And the relationship would
be Address belongs_to :person, Person has_many :addresses. If you
still want to keep them separate and have the address separate from
the person, then if whichever one you want to carry the ID would have
to have the belongs_to association to the other table. And the other
table would have the "has_one" association.

> --
> You received this message because you are subscribed to the Google Groups "rubyamf" group.
> To post to this group, send email to rub...@googlegroups.com.
> To unsubscribe from this group, send email to rubyamf+u...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/rubyamf?hl=en.
>
>

Brent

unread,
Oct 12, 2010, 9:24:46 AM10/12/10
to rubyamf
This is a good point. I will have numerous models in this app that
will had addresses. I just figured that it would be easier to have one
address model/table rather than a bunch of addresses all over the
place. I'm enthused that there is such quick participation here. Any
advice will definitely be considered.

Thanks,
Brent

Victor Castell

unread,
Oct 12, 2010, 4:17:48 PM10/12/10
to rub...@googlegroups.com
Why you don't define the association the other way? 

Person 
  belongs_to :address

Address
  has_one :person
  has_one :modeln

So you end up with a reference in pople table

--
www.victorcoder.com | www.season.es | @victorcoder

Brent Bonet

unread,
Oct 13, 2010, 9:35:19 AM10/13/10
to rub...@googlegroups.com
I did. Of course this means a personId, modelnId in my address model
but I can live with that. After generating mappings I removed the
association for people in address which prevents address from loading
the person from the database which is unnecessary.

Thanks for the tips everyone!

Brent

Reply all
Reply to author
Forward
0 new messages