rails HABTM versus view (formtastic)

Posted by VP on Stack Overflow See other posts from Stack Overflow or by VP
Published on 2010-03-29T19:36:15Z Indexed on 2010/03/30 3:13 UTC
Read the original article Hit count: 374

Filed under:
|

I have two models:

The model NetworkObject try to describe "hosts". I want to have a rule with source and destination, so i'm trying to use both objects from the same class since it dont makes sense to create two different classes.

class NetworkObject < ActiveRecord::Base
  attr_accessible :ip, :netmask, :name
  has_many :statements
  has_many :rules, :through =>:statements
end

class Rule < ActiveRecord::Base
  attr_accessible :active, :destination_ids, :source_ids
  has_many :statements
  has_many :sources, :through=> :statements, :source=> :network_object
  has_many :destinations, :through => :statements, :source=>  :network_object
end

To build the HABTM i did choose the Model JOIN. so in this case i created a model named Statement with:

class Statement < ActiveRecord::Base
  attr_accessible :source_id, :rule_id, :destination_id
  belongs_to  :network_object, :foreign_key => :source_id
  belongs_to :network_object,  :foreign_key => :destination_id
  belongs_to :rule
end

The problem is: is right to add two belongs_to to the same class using different foreign_keys? I tried all combinations like:

belongs_to :sources, :class_name => :network_object, :foreign_key => :source_id

but no success.. anything that i am doing wrong?

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about habtm