0

I have overwritten a getter in one of my models for a has_many relationship. The use case for this is that it builds 'fake' (non-persisted) objects that are scheduled tasks, purely for the purpose of displaying them on a calendar. For example, task A is a persisted record, and has a list of its future occurrences. The getter for this relationship will get the original object, and build any repeated instances of the task from the list of future times, returning them all at once.

Because the overwritten getter returns an array instead of a relation, I cannot call .build on this getter in a simple_form form. E.g.

instance_of_model.overwritten_getter.build

will fail, as you cannot call .build on an array.

Is there a way to write the overwritten getter in such a way that it will allow this? If not, is there a preferred design pattern for doing something like this?

1 Answers1

0

I ended up solving this by calling .new on the model that had the overwritten getter, and providing the parent ID explicitly, in the form's declaration.

Instead of form_for (@user.tasks.build)

It became form_for (Task.new(user: @user))