Rails seed JSON ignore some attributes -


i using rails json gem seed database json data. in seeds.rb file, have method

  businesspath = "#{rails.root}/public/business2.json"   businesses = json.parse(file.read(businesspath))   businesses.each |business|   business.create!(business)     end 

however, json data given few attributes not want model business have. when try seed is, error.

can't mass-assign protected attributes: schools, categories, neighborhoods, longitude, latitude, type 

those attributes don't have in business model in rails attributes each business in json file. there way ignore attributes before running business.create?

thanks!

try hash#slice or hash#except activesupport provides. can run:

businesspath = "#{rails.root}/public/business2.json" businesses = json.parse(file.read(businesspath)) 

and can blacklist attributes

businesses.each |business|   business.create!(business.except(:schools, :categories, :neighborhoods, :longitude, :latitude, :type)) end 

or whitelist attributes want keep

businesses.each |business|   business.create!(business.slice(:name, :owner, :etc)) end 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -