[英]Ruby on Rails - Modelling a Directed Graph
我正在嘗試為我的Rails應用程序上的紅寶石建模一個有向圖,我有兩個模型,標記和連接
class Connection < ActiveRecord::Base
attr_accessible :cost, :from_id, :to_id
belongs_to :from_tag, :foreign_key => "from_id", :class_name => "Tag"
belongs_to :to_tag, :foreign_key => "to_id", :class_name => "Tag"
end
class Tag < ActiveRecord::Base
attr_accessible :location_info, :reference
has_many :to_connections, :foreign_key => 'from_id', :class_name => 'Connection'
has_many :to_tags, :through => :to_connections
has_many :from_connections, :foreign_key => 'to_id', :class_name => 'Connection'
has_many :from_tags, :through => :from_connections
end
當我像這樣創建標簽時
a = Tag.create(:reference => "a", :location_info => "Tag A")
b = Tag.create(:reference => "b", :location_info => "Tag B")
工作正常。
但是當我試圖在兩者之間建立聯系時
Connection.create(:from_tag => a, :to_tag => b, :cost => 5)
我說錯了
“ ActiveModel :: MassAssignmentSecurity :: Error:無法大量分配受保護的屬性:from_tag和to_tag”
,有人可以看到問題嗎?
您無法大規模分配關系。
http://api.rubyonrails.org/classes/ActiveModel/MassAssignmentSecurity/ClassMethods.html
connection = Connection.new
connection.from_tag = a
connection.to_tag = b
connection.cost = 5
connection.save
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.