我正在尝试为rails应用程序上的ruby建模有向图 我有两个模型,标签和连接
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“
,有人能看到问题吗?
答案 0 :(得分:1)
你无法大规模分配关系。
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