开发者

Serializing ActiveRecord objects without storing their attributes?

开发者 https://www.devze.com 2022-12-25 04:28 出处:网络
I\'m working on a problem where I need to store serialized hierarchies of Ruby objects in the database.Many of the objects that will need to be saved are ActiveRecord objects with a lot of attributes.

I'm working on a problem where I need to store serialized hierarchies of Ruby objects in the database. Many of the objects that will need to be saved are ActiveRecord objects with a lot of attributes. Instead of saving the entire objects and then refreshing their attributes from the DB when I load them (in case they changed, which is likely), it would be easier to just store the references (class and database id) for these objects.

Does anyone know if there's already a way to do this in Rails, or if there's an existing gem for it? Wanted to check if something exis开发者_开发知识库ted before spending a ton of time hacking on it.


As far as I understand your problem you should google for: acts_as_nested_set better_nested_set even_better_nested_set and awesome_nested_set. I know they don't store the serialized hierarchies, but you should store their nodes atomically. Even huge traffic sites do that. Other than that - you should consider NoSQL (or schema-less DB).


As far as I understand your problem you should add to you model :serialize field_name and push all attributes nested object to this field. Rails will serialize this object using gem Marshal.

There are some example of serializing using Marshal without rails and ActiveRecord. And this - is answer on Stack Overflow, how does it work.

0

精彩评论

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

关注公众号