makandra / active_type

Make any Ruby object quack like ActiveRecord
MIT License
1.09k stars 74 forks source link

How to make existing nested records read_only? #60

Closed lastobelus closed 8 years ago

lastobelus commented 8 years ago

I'm having some trouble figuring out how to implement the following business logic with nests_one (or with accepts_nested_attributes_for using plain activerecord):

An Order has an Organization. When creating an order, the user can fill out organization attributes, two of which are nickname & email. However, if either nickname or matches an existing organization it should be attached to order, but not edited, which I wish to enforce in the model.

Similarly, the Organization has a Contact, with natural primary key email.

Is there a clean way to build this up with the existing ActiveType api using nests_one, or would I be better off just to use attribute organization_attrs etc. and implement the logic in the AR callbacks?

To sum it up: how do I make nested attributes act as find-or-create rather than find-and-update-or-create?

triskweline commented 8 years ago

AT's nested attributes were built to only cover the basic amenities provided by Rails' original nested attributes implementation. The functionality you described will need to be implemented in custom callbacks.