开发者

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 开发者_Go百科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. e.g.

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜