Forum: Ruby on Rails circular DB problem

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
7dd10e685d07b10fa95a4b8c33387922?d=identicon&s=25 Simon (Guest)
on 2007-07-31 15:34
(Received via mailing list)
Hi,

Wondering if anybody could give me a hand.  I have three tables,
operators, operator_aps, and pingable_ips with the following
relationships:

operator has_many operator_aps
operator has_many pingable_ips

Now, what I need to do is relate one of the operator's pingable_ips
with each of their operator_aps.  I'm not sure what the best way to go
about this is, or how to properly define it with the has_many/
belongs_to relationships.  I was thinking of just doing:

operator_ap has_one pingable_ip

but then pingable_ip belongs_to both operator and operator_ap, and
operator_ap already belongs_to an operator.  Not sure if this will
cause any issues, especially when deleting records from these tables.

If anybody has any advice on a nice way to set these relationships up,
it would be greatly appreciated.

Thanks,

Simon
457cf540784a12ba2f30e06565a2c189?d=identicon&s=25 Hugh Sasse (Guest)
on 2007-07-31 15:56
(Received via mailing list)
On Tue, 31 Jul 2007, Simon wrote:

> Now, what I need to do is relate one of the operator's pingable_ips
> with each of their operator_aps.  I'm not sure what the best way to go
> about this is, or how to properly define it with the has_many/
> belongs_to relationships.  I was thinking of just doing:
>
> operator_ap has_one pingable_ip
>
> but then pingable_ip belongs_to both operator and operator_ap, and
> operator_ap already belongs_to an operator.  Not sure if this will
> cause any issues, especially when deleting records from these tables.

I think it depends on the relationship between pingable_aps and
pingable_ips.  If you allocate the ip to the app depending on
whether it is one of the operator's ip addresses, then break the
association between the operator and the app. You can always tell
the ip address to do things to the apps it owns. Otherwise, if you
allocate the ap depending on whether it is going to the operator's
app, break the association between the operator and the ip.
Basically, they should not be poking in each other's pockets so
much.   If it is one ip per app, you can always ask the apps to
handle the IP issues.

As a third choice, the operator keeps the mapping between
her apps and her ips, and the two need never know about this
directly.

Circular graphs make recursion too ... er, interesting...
>
> If anybody has any advice on a nice way to set these relationships up,
> it would be greatly appreciated.
>
> Thanks,
>
> Simon
>
    HTH, though I don't understand your problem domain in detail.
    Hugh
391f9b787cdc12aa2c179713f5103e3a?d=identicon&s=25 Ilan Berci (iberci)
on 2007-07-31 17:01
Simon wrote:
> Hi,
>
<snip/>
>
> operator has_many operator_aps
> operator has_many pingable_ips
>
<snip/>
>
> operator_ap has_one pingable_ip
>
> but then pingable_ip belongs_to both operator and operator_ap, and
> operator_ap already belongs_to an operator.  Not sure if this will
> cause any issues, especially when deleting records from these tables.
>
> If anybody has any advice on a nice way to set these relationships up,
> it would be greatly appreciated.
>
> Thanks,
>
> Simon

Simon!  (cool to see you on this list)

If I understand you correctly, this is what you have..

class Operator < ActiveRecord::Base
  belongs_to :operator_app
  has_many :pingable_ips
end

class OperatorApp < ActiveRecord::Base
  has_many :operators
  has_many :pingable_ips
end

class PingableIp < ActiveRecord::Base
  belongs_to :operator_app
  belongs_to :operator
end

You should be fine when it comes to destroying records,  the thing to
watch for is how you set up your foreign key constraints!   (Just watch
for the caveat at the bottom.)

>> oa = OperatorApp.new
=> #<OperatorApp:0x3466128 @new_record=true, @attributes={}>
>> oa.operators << Operator.new
=> [#<Operator:0x3464224 @new_record=true,
@attributes={"operator_app_id"=>nil}>]
>> oa.pingable_ips << PingableIp.new
=> [#<PingableIp:0x345ec98 @new_record=true,
@attributes={"operator_id"=>nil, "operator_ap_id"=>nil}>]
>> oa.pingable_ips.first.operator = oa.operators.first  # <- the "circular" reference you 
are concerned about
=> #<Operator:0x3464224 @new_record=true,
@attributes={"operator_app_id"=>nil}>
>> oa.save
=> true
>> oa.destroy
=> #<OperatorApp:0x3466128 @errors=#<ActiveRecord::Errors:0x3456278
@errors={}, @base=#<OperatorApp:0x3466128 ...>>, @new_record=false,
@pingable_ips=[#<PingableIp:0x345ec98
@errors=#<ActiveRecord::Errors:0x3456048 @errors={},
@base=#<PingableIp:0x345ec98 ...>>, @new_record=false,
@operator=#<Operator:0x3464224 @errors=#<ActiveRecord::Errors:0x3455e04
@errors={}, @base=#<Operator:0x3464224 ...>>, @new_record=false,
@attributes={"id"=>2, "operator_app_id"=>2},
@new_record_before_save=true>, @attributes={"operator_id"=>2, "id"=>2,
"operator_ap_id"=>nil, "operator_app_id"=>2}>], @attributes={"id"=>2},
@new_record_before_save=true, @operators=[#<Operator:0x3464224
@errors=#<ActiveRecord::Errors:0x3455e04 @errors={},
@base=#<Operator:0x3464224 ...>>, @new_record=false,
@attributes={"id"=>2, "operator_app_id"=>2},
@new_record_before_save=true>]>

>> OperatorApp.find(:all)
=> []
>> Operator.find(:all)
=> [#<Operator:0x34400cc @attributes={"id"=>"2",
"operator_app_id"=>"2"}>]
>> PingableIp.find(:all)
=> [#<PingableIp:0x343d764 @attributes={"operator_id"=>"2",
"operator_ap_id"=>nil, "id"=>"2"}>]
>>

just notice the orphans with the last 2 statements and just make sure to
clean them up in your models with the appropriate filter

hth

ilan
7dd10e685d07b10fa95a4b8c33387922?d=identicon&s=25 Simon (Guest)
on 2007-07-31 19:34
(Received via mailing list)
Hi,,

That was close to what I meant, but here is what I was trying to get
at:

class Operator < ActiveRecord::Base
  has_many :operator_app
  has_many :pingable_ips
end

class OperatorApp < ActiveRecord::Base
  belongs_to :operators
  has_one :pingable_ips
end

class PingableIp < ActiveRecord::Base
  belongs_to :operator_app
  belongs_to :operator
end

I just don't like how those relationships are circular, with the
PingableIp belonging to both the Operator and the OperatorApp, when
the OperatorApp also belongs to an Operator.  But so far I haven't
come up with a clever way to handle those relationships differently.
Basically, the Operator has many OperatorApps and PingableIps, and
each single OperatorApp needs to be associated with a single
PingableIp that also belongs to that Operator.  The same PingableIp
can be related to several OperatorApps.

Maybe I'm missing something obvious, or just thinking about the
relationships the wrong way, but there must be a cleaner way to get
this set up.

Thanks again,

Simon

On Jul 31, 11:01 am, Ilan Berci <rails-mailing-l...@andreas-s.net>
0097752cedcb6172fc967e850d31ff3f?d=identicon&s=25 Dondi Stroma (dmspilot00)
on 2007-07-31 21:43
Simon wrote:
> Hi,,
>
> That was close to what I meant, but here is what I was trying to get
> at:
>
> class Operator < ActiveRecord::Base
>   has_many :operator_app
>   has_many :pingable_ips
> end
>
> class OperatorApp < ActiveRecord::Base
>   belongs_to :operators
>   has_one :pingable_ips
> end
>
> class PingableIp < ActiveRecord::Base
>   belongs_to :operator_app
>   belongs_to :operator
> end
>
> I just don't like how those relationships are circular, with the
> PingableIp belonging to both the Operator and the OperatorApp, when
> the OperatorApp also belongs to an Operator.  But so far I haven't
> come up with a clever way to handle those relationships differently.
> Basically, the Operator has many OperatorApps and PingableIps, and
> each single OperatorApp needs to be associated with a single
> PingableIp that also belongs to that Operator.  The same PingableIp
> can be related to several OperatorApps.

Hi,

The API documentation states that belongs_to is used on a model class
when you have foreign keys on the model's table. So what does your
pingable_ips table look like? The way your PingableIp class is setup now
implies that the pingable_ips table has a operator_app_id and an
operator_id.  But the operator_apps table would also have an
operator_id, so it's actually redundant (I wouldn't really call it
circular).

Since you don't need the foreign key, I believe that means you shouldn't
(and can't) have the belongs_to :operator in your PingableIp. In your
Operator class, you can put has_many :pingable_ips, :through =>
:operator_app.

(It's like the Firm/Client/Invoice example in the "Association Join
Models" section of the ActiveRecord::Assocations::ClassMethods
documentation. See
http://api.rubyonrails.org/classes/ActiveRecord/As...)
This topic is locked and can not be replied to.