1

Ruby on Rails アプリケーションの有向グラフをモデル化しようとしています。Tag と Connection の 2 つのモデルがあります。

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"

、誰でも問題を見ることができますか?

4

1 に答える 1

2

You cannot mass-assign relations.

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
于 2013-03-04T12:16:45.800 に答える