Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

One of depending on another model's column #113

Open
andresespinosapc opened this issue Mar 2, 2022 · 4 comments
Open

One of depending on another model's column #113

andresespinosapc opened this issue Mar 2, 2022 · 4 comments
Labels
enhancement New feature or request

Comments

@andresespinosapc
Copy link

I want to use a different StoreModel depending on another column in the parent model. For example, I have a model Order that has an attribute provider_name which can be shopify or magento. It has another attribute provider_shipping_info that is a jsonb and I want it to have a different StoreModel depending on the provider_name attribute.

Is it possible?

@DmitryTsepelev
Copy link
Owner

Hi @andresespinosapc! Currently it's not possible (see OneOf docs), but it might be a useful feature 🙂

@DmitryTsepelev DmitryTsepelev added the enhancement New feature or request label Mar 5, 2022
@aribouius
Copy link

I have been trying to figure out how to do this for ages, unfortunately Rails just doesn't appear to expose a good way to do it.

@seanabrahams
Copy link

I have a slightly different use case that likely requires a similar solution.

Given a store_model_class instance method on an ActiveRecord model that returns a dynamically generated StoreModel::Model class, I'd like to do:

attribute :data, proc { store_model_class.to_type }

The issue seems to be that the binding of the proc is the class instead of an instance of the class. This makes sense considering attribute is a class method.

The OneOf implementation is interesting because it allows you to have logic that targets the model instance's JSON data but not the model instance itself. The question is, how do we get the binding of the proc to be the instance of the model or at least include the instance of the model somewhere?

After some digging it's not clear how this can be achieved.

@DmitryTsepelev any pointers would be appreciated. Thanks.

@MilanVilov
Copy link

I have been digging into this problem as well, but the issue for me is at what point in time should you actually include the StoreModel, you could do it on the setters for that other fields, but that is not enough as setter are not always used. Ideally you would set the when the data is loaded into the instance of the object, so you can alter the objects singleton class.
Any idea what even we could maybe monkey patch with a callback?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

5 participants