Closed zedtux closed 2 years ago
This PR adds automatically 2 new fields which should have an index. Doing so would raise the error:
NoBrainer::Error::MissingIndex: Please run `NoBrainer.sync_indexes' or `rake nobrainer:sync_indexes' to create the index `imageable__id__imageable_type` in the table `nobrainer_test.pictures`. Read http://nobrainer.io/docs/indexes for more information.
This is completely normal and actually works like that with ActiveRecord when generating the migration script.
Thank you for all comments, I'll rework that soon. 🙇
The following case doesn't seem to work:
define_class :Metadata do
include NoBrainer::Document
end
define_class :Picture, Image do
include NoBrainer::Document
has_many :metadatas
end
define_class :Event do
include NoBrainer::Document
belongs_to :restaurant
has_many :photos, as: :imageable, class_name: 'Picture'
has_many :metadatas, through: :photos
end
Trying to access event.metadatas
fails with:
Traceback (most recent call last):
1: from (irb):1
NoMethodError (undefined method `metadatas' for #<Picture:0x00007f8eed0fcb38>)
Did you mean? photos
This PR implements the polymorphic associations, the same way ActiveRecord does.
belongs_to
now accepts thepolymorphic: true
andhas_many
/has_one
theas: :xxx
. Nobrainer adds 2 fields to the polymorphic model underthehood to store the foreign ID and Class then reuse them in query criteria.