tamouse m. wrote in post #1111882:
> see the author_name up there?
>
> When you save the post, that's when it calls the #set_author method,
> which either finds an existing author with the name given, or creates a
> new one, which is connected to the new post record with self.author.
It has been quite a break-through for me today :)
I used to do queries step by step, one by one, outside of model and then
assemble results together in one final Mode.create/save() method. All is
done in transaction but outside a model.
But isn't it strange, isn't it quite illogical that one model is
responsible for creating an absolutely different other model? In this
example the Post model is responsible for creating an instance of the
User model. And more than that, all the logic of creating a new User
instance is located inside of the Post model. It is against the idea of
AR pattern where each model is responsible for its own behavior.
But it is a rhetorical question from me. The way Rails offers do save
objects is pretty convenient. I'm glad I've understood it today.
> You *can* do it the other way around, but it's clearly more work, since
> ActiveRecord/Arel is doing all that for you.
I should read more about this Arel feature.
Thank you.