Is there anything similar to ruby preferences in elixir?

I’m exploring this library I want to know if there is anything with similar features we have in the elixir?

https://github.com/pluginaweek/preferences

Defining such fields in your Ecto schema? I do not quite get what this library does except providing additional API on top of fetching fields from the DB.

The readme suggests this is basically just a bunch of accessors + change tracking for a map field. Sounds like similar things can be done with ecto changesets + some custom functions for the accessors (maybe even an implementation of the access protocol).

Eh. Last commit: 9 yrs ago. Build: failing. Do we really need something like this is Elixir?

:man_shrugging:

2 Likes

Isn’t embedded schema perfect for this?

1 Like

Hi @siddhant3030!

As others have pointed out, you may want to look at Ecto. We often use it for databases, but its schemas can be used to contain any data and a changeset can be used to track changes.

If this is not enough, please try to provide more information about which problem you want to solve with said tool.

Please let’s try to be more helpful. We can safely assume he doesn’t want an outdated solution or a failing build, but something that solves a particular problem. Thank you. :slight_smile:

18 Likes

I will explain the problem me and @siddhant3030 have in hand

We need to store some key value pairs in a column called preference in our postgresql DB.But the keys varies in different cases. The keys for each case is predefined. How can we achieve this?

You might want to read about polymorphic associations here under polymorphic association and there : https://hexdocs.pm/ecto/polymorphic-associations-with-many-to-many.html.

You might also be able to store the preferences as a map depending on your use case.

3 Likes

But this is for associations and we don’t have any kind of associations in this particular case.

We want to store key value pairs in a column, and the keys varies in each case. We need to be able to do some validation on the keys. I don’t think we can use embedded_schemas because there is only one column for all cases

Replicating that exact functionality is going to be complicated, since the gem generates a preferences table with two polymorphic associations (not supported natively in Ecto).

HOWEVER, there are better ways to store this data now than we had in 2011. For instance, JSON columns. Even in an ActiveRecord context I’d probably reach for ActiveRecord::Store which uses a single JSON column to store all of a record’s preferences.

Consider an embedded schema Preferences on User to deal with loading / storing / casting this data.

2 Likes

I haven’t used it myself but maybe Polymorphic embeds do what you want.

1 Like

This is really interesting. Thanks

Also for these runtime dynamic validations the norm library can be used in a very flexible way