简体   繁体   中英

How to get ar_fixtures to load protected attributes?

I'm using ar_fixtures to seed data in a rails project (the procedure was written before seed arrived in 2.3.4)

It is all working fine, except I find that one column in particular is not getting populated (user_id). user_id is set in the yaml file, for example:

- !ruby/object:Service 
  attributes: 
    name: name
    updated_at: 2009-10-14 11:50:36
    provider_id: "1"
    id: "1"
    description: ""
    user_id: "1"
    created_at: 2009-10-14 11:47:01
  attributes_cache: {}

But even though the related user object exists when running the import (Service.load_from_file), user_id is nil after import. Other foreign keys (like provider_id in the example above) are loaded correctly.

I am suspecting this is because user_id is protected in the model and this getting blocked during mass assignment.

Does anyone know if this is the case, and if so, how to get around the mass assignment protection? Of course, I want to leave mass assignment protection in place for the application at runtime.

Fixed! Answering my own question..

Simply requires a tweak of the attr_protected / attr_accessible setting prior to loading. eg

Service.attr_protected.delete "user_id"
Service.load_from_file

Or if the restriction is based on attr_accessible:

Service.attr_accessible :user_id
Service.load_from_file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM