Skip to content

Commit 8f42990

Browse files
wolfoo2931Emily Giurleop
authored
Fix MONGOID-4952 Unable to access embedded fields in projected query results (#4843)
* Prevent 'Missing attribute' when accessing attributes of embedded docs * add test for the missing attriubte bug * fix type in test * add a new test and fix small bug * make comment clearer * added more cases to tests * save! * association * verify that projecting an association and its field is not allowed * redo fix to be in line with what the method is supposed to do * handle 4.2 and 4.4 separately * reorganize tests * handle different order of projection of association vs field * fix 4.2 and lower servers * add doc note Co-authored-by: Emily Giurleo <e.m.giurleo@gmail.com> Co-authored-by: Oleg Pudeyev <oleg@bsdpower.com>
1 parent 10d5bb8 commit 8f42990

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

source/tutorials/mongoid-queries.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,25 @@ should therefore be avoided.
778778
band.tours.first
779779
# => #<Tour _id: 5c59afdf026d7c034dba46af, city: nil, year: 1995>
780780

781+
.. note::
782+
783+
Server versions 4.2 and lower allowed projecting both an association and
784+
the association's fields in the same query, as follows:
785+
786+
.. code-block:: ruby
787+
788+
band = Band.only(:tours, 'tours.year').last
789+
790+
The most recent projection specification overrided the earlier one.
791+
For example, the above query was equivalent to:
792+
793+
.. code-block:: ruby
794+
795+
band = Band.only('tours.year').last
796+
797+
Server versions 4.4 and higher prohibit specifying an association and its
798+
fields in projection in the same query.
799+
781800
``only`` can be specified with referenced associations (has_one, has_many,
782801
has_and_belongs_to_many) but is currently ignored for referenced associations -
783802
all fields of referenced associations will be loaded

0 commit comments

Comments
 (0)