Nested attributes and deep references

I’m using nested attributes in a new project, but struggling with one

Imagine, for example you have the models User, Book, and Shelf. A user
has_many books, and has_many shelves. A shelf also has_many books.
User accepts_nested_attributes for books and shelves, and shelves accept
them for books.

So, if I were to do something like the following pseudo code:

shelf = @user.shelves.find([some conditions])
book =[some params])

In this case, the book should be correctly associated with the shelf,
but it’s not associated with the user. Is there a way to automatically
have the book be associated with the user that the shelf was associated
with when the book is created like this? Perhaps through a callback?

Just wondering if there’s a standard way to do this before I go
re-inventing anything.

Thanks :slight_smile: