-1
我有兩個模型:rails HABTM vs view(formtastic)
模型NetworkObject試圖描述「主機」。我想要一個有源和目標的規則,所以我試圖使用來自同一個類的兩個對象,因爲它沒有意義創建兩個不同的類。
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
要建立HABTM我做了選擇模型JOIN。所以在這種情況下,我創建了一個名爲聲明型號:
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
的問題是:是正確的belongs_to的添加兩個使用不同foreign_keys同一類?我試過所有的組合如:
belongs_to :sources, :class_name => :network_object, :foreign_key => :source_id
但沒有成功..什麼,我做錯了什麼?
這是我的嘗試之一。沒有工作。我想也許:源是一個保留字..無論如何,我修復它創建兩個模型Dst和Src,然後我從NetworkObject做遺產。這是污垢,但工作:-) – 2010-03-30 06:38:43