Closed theonesean closed 11 months ago
Hi, thanks.
Does the problem reproduce if you JUST run your example code on its own, outside of "seeding", or only when seeding?
If it's only when seeding, can you make a little sample Rails app that can demonstrate the problem, that I can run? If I have a reproduction I can try to underestand the problem and fix it, but if you could help create the reproduction that would be hugely helpful, to save me the time of doing so without knowing for sure how to get to what you're doing.
Especially because I don't personally use Rails "seeds" feature so am not certain how it works and would have ot research it -- if you can instead just give me and app and tell me "run db:setup on this app to see the problem", it would give me a real head start and let me spend my time focusing on understanding and hopefully solving the problem, instead of setting up the reproduction!
The smallest possible case that reproduces the problem is especially helpful. Like I only see one attr_json
there with array:true
-- if we remove all the other ones, does the problem still reproduce?
Also -- what version of attr_json, is it the latest?
Hi, @jrochkind, thanks for the quick reply!
Does the problem reproduce if you JUST run your example code on its own, outside of "seeding", or only when seeding?
There’s nowhere in the app that I wholesale create!
an instance of SiteMetum
outside of seeding, so I’m not 100% sure.
can you make a little sample Rails app that can demonstrate the problem, that I can run?
Absolutely! I will try to put one together on Monday when I’m back at the office — travelling this weekend.
if we remove all the other ones, does the problem still reproduce?
Yes, it reproduced even with only that use of attr_json
.
what version of attr_json, is it the latest?
Yep, latest version.
Thanks so much! Appreciate it.
Hey @theonesean , I found some time and managed to duplicate.
The problem seems to be caused by this validation:
validates :social_entries, uniqueness: { scope: :name, message: "must have only one entry per account type" }
If you remove that validation, you no longer have the problem. Can you verify this?
I think you just can't use ActiveRecord uniqueness
validations with attr_json array's -- I'm not looking at the AR implementation right now, but I suspect it's written in specific ActiveRecord database calls, and tries to make sure the value of the attribute as a whole is unique across the table ... and can't "just work" for an attr_json array -- especially not the way you were hoping scope
would magically transfer to an embedded attr_json array with analogous semantics!
I am inclined not to consider this a bug in attr_json; I'm not sure there's much that could be done about it. You just have to write your own custom validation to make sure the values within the attr_json array are unique. I think that should be fairly easy to do, if the attr_json model's implement ==
equality usefully, which I think they do? Bonus, you won't have to make a trip to the database to ensure unqiueness, like the ActiveRecord one does.
If you come up with such a custom validation, and want to package it up as easily re-usable and submit it as a PR here (with tests and docs included), I'd be happy to review!
Let me know what you think of all this, thanks.
Ahh, cool, thank you so much for reproducing! Strange how that validation would cause an issue -- the error at hand was super super cryptic.
I might give custom validation a go. Thanks!
On Mon, 4 Dec 2023 at 10:12, Jonathan Rochkind @.***> wrote:
Hey @theonesean https://github.com/theonesean , I found some time and managed to duplicate.
The problem seems to be caused by this validation:
validates :social_entries, uniqueness: { scope: :name, message: "must have only one entry per account type" }
If you remove that validation, you no longer have the problem. Can you verify this?
I think you just can't use ActiveRecord uniqueness validations with attr_json array's -- I'm not looking at the AR implementation right now, but I suspect it's written in specific ActiveRecord database calls, and tries to make sure the value of the attribute as a whole is unique across the table ... and can't "just work" for an attr_json array.
I am inclined not to consider this a bug in attr_json; I'm not sure there's much that could be done about it. You just have to write your own custom validation to make sure the values within the attr_json array are unique. I think that should be fairly easy to do, if the attr_json model's implement == equality usefully, which I think they do? Bonus, you won't have to make a trip to the database to ensure unqiueness, like the ActiveRecord one does.
If you come up with such a custom valication, and want to package it up as easily re-usable and submit it as a PR here (with tests and docs included), I'd be happy to review!
Let me know what you think of all this, thanks.
— Reply to this email directly, view it on GitHub https://github.com/jrochkind/attr_json/issues/217#issuecomment-1838851510, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABJJT6GUNHN4D2HJO6LHA3DYHXR47AVCNFSM6AAAAABADK6ZTKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQMZYHA2TCNJRGA . You are receiving this because you were mentioned.Message ID: @.***>
The error is trying to tell you that the ActiveRecord uniqueness validation does not know how to do what it does with the AttrJson::Array type that is the value of the attribute.
If it got past that error somehow, it would still complain that it doesn't know how to do what it does with an attribute that does not map to a column... if it did know how to do it, what it owuld do is make sure no other record in the table has the exact same value as this one, since that's what the uniqueness validation does!
AttrJson is great at helping you forget this isn't actually the same as an AR to-many association for many purposes... but it really isn't!
So to me not that strange at all once you think through it, although I'd agree the error ended up cryptic!
Hi! I'm using this package for a CMS-type thing -- I have a model with an array of social media entries. One issue: when I create an instance of the class for seeding, I inevitably run into
TypeError: can't cast Array
-- the only thing that addresses the issue is removingarray: true
from the attribute definition, which defeats the purpose, because I'd like the attribute to be an array of Socials.Creating them via hash or array of Social instances both fail, despite referencing the README section here. Just not sure what I'm doing wrong! Thanks for taking a look.
Here's the relevant code and stacktrace:
Model:
seeds file:
stacktrace (I've previously dropped the database -- starting fresh here):