I'm building an association to version control edits of, essentially,
an array. I thus have a parent table, called, say "books". This has
an id and a current edit version. There is another table called, say,
"chapters". Each chapter belongs to a book and hence contains a
book_id. Each chapter also has an edit_version. So, if you want a
specific version of a book, you grab all chapters with a specified
Thus, most of the time we are only interested in chapters whose
edit_version matches the book edit_version.
One way or another, it seems clear that the book model wants to specify
that a book has_many chapters. It then seemed reasonable to suggest
that when eager fetching chapters for a book, we should attach a
:constraint to the has_many association to only fetch those chapters
with the same edit_version as the book.
This worked great until I went to save the book and chapters. At that
point, rails generated code that looked something like "update chapters
where id = 14 and (chapters.edit_version = books.edit_version)".
This appears to be a bug. [Of course, the powers that control the bug
list don't like bugs to be reported and don't like trying to decide if
something is a bug or not and so rejected this suggestion that there
might be a bug out of hand. (Why do people make publicly accessible
bug lists if they are just going to ignore the crowdsourced bug reports
and get all snotty when people don't want to read the entire bug
database to see if someone has commented on the issue before?)] The
specific bug here is: if you know exactly which record you are going
to update (and you do because you are specifying the 'id' field and the
whole point to that field is so that you can exactly reference a
specific record) why would you drag in the :constraints field from the
Then there's the enhancement request portion of this. If we have an
array hanging off of an object, and we typically want to work with a
small slice of the array, is it reasonable to have an association that
works with that type of structure?
And, finally, there's the documentation enhancement request portion of
this. There must be a way to describe in the documentation the real
purpose of the :constraints field. It is not at all clear from the
documentation that you can use the constraint to force records in the
child table to match records in the parent table. It's also not clear
that the constraint will be pulled in when updating records. So there
is some meta-reasoning and meta-logic that in someone's mind about what
the constraint is and does that is not being documented.