solnic / virtus

[DISCONTINUED ] Attributes on Steroids for Plain Old Ruby Objects
MIT License
3.77k stars 228 forks source link

Virtus.model(strict: true) blows up while building with Factory Girl #271

Closed dredozubov closed 9 years ago

dredozubov commented 10 years ago

I'll try to illustrate it with a gist: https://gist.github.com/dredozubov/594bf466b2477fef28ba I find this behaviour unexpected and quite strange. Not sure where the source of the problem lies, it works just fine without strict: true. Can anyone shed some light on this issue?

solnic commented 9 years ago

I'm not sure if FG is supposed to work with Virtus models. Please report an issue in the FG project.

dkniffin commented 8 years ago

In case anyone else runs into this issue, I've got a bit more info, and a solution.

I ran into a couple issues with using factory_girl and virtus. The first was something like:

private methodattribute=' called for #Model:0x007f92329f3438`

I tried resolving this by adding attr_accessor for each of my attributes, but that caused more issues, because factory_girl was basically bypassing virtus's logic.

My ultimate solution was to add this to my factory:

    skip_create
    initialize_with { new(attributes) }

The first line ensures factory_girl doesn't call .save!. The second line tells factory_girl to pass the attributes into the new methods, instead of trying to assign them via accessors (attribute=).

I hope this helps someone in the future.