문제는 내가이 오류를 얻을 수 있습니다 :기계화 대량 할당 오류에 가입 테이블
ActiveModel::MassAssignmentSecurity::Error: Can't mass-assign protected attributes: amenity_id
나는이 코드를 실행하면
task import_amenities: :environment do
agent = Mechanize.new
Kindergarten.find_all_by_public(false).each do |k|
p = agent.get(k.uri)
amenities = p.search("td td tr:nth-child(11) td:nth-child(2)").text.split(/(;|,) */)
amenities.each do |a|
am = Amenity.find_or_create_by_name!("#{a}")
k.update_attributes(amenity_id: am.id)
end
end
end
유치원과 편의 시설이 HABTM 관계를 통해 연결되어 및 다음과 같이 정의된다 :
kindergarten.rb
class Kindergarten < ActiveRecord::Base
attr_accessible :location, :name, :public, :uri, :address, :contact,
:phone, :url, :email, :description,
:password, :password_confirmation, :amenity_ids
has_and_belongs_to_many :amenities
end
여기 amenity.rb
class Amenity < ActiveRecord::Base
attr_accessible :name, :kindergarten_ids
has_and_belongs_to_many :kindergartens
end
및
조인 테이블에 대한 마이그레이션입니다 :
class CreateKindergartensAmenitiesJoinTable < ActiveRecord::Migration
def up
create_table :kindergartens_amenities, :id => false do |t|
t.integer :kindergarten_id
t.integer :amenity_id
end
end
end
오류는 레이크 작업에서이 라인으로 인해 발생 :
k.update_attributes(amenity_id: am.id)
모든 것 같습니다 질량 할당에 도달 할 때까지 콘솔에서 잘 작동합니다. 그리고 나는 HABTM 이전에는 한번도 사용 해본 적이 없기 때문에 내가 여기있는 것을 정말로 엉망으로 만들고 있다고 생각합니다.
의견이 있으십니까?