I have a base model which subclasses Ohm::Model, my other models would subclass this submodel. In addition in my submodel i have an attribute common to all other models that I furthermore want to have in index created on this attribute. This would allow me to have a consistent way for all models to be found by this property.
HOWEVER, this does not seem to be working as Ohm just skips creating the necessary index, unless I define the index inside of the child class but referring to the inherited property. this works but does not appear elegant as all of my child classes would have to have this declaration. See below:
# base model note i'm using ohm/contrib plugins in the base class and so all of my derived classes thus further making this design more beneficial/efficient
class BaseModel < Ohm::Model
include Ohm::Timestamps
include Ohm::DataTypes
include Ohm::Callbacks
attribute :uid
index :uid
def before_create
self.uid = SecureRandom.uuid
end
end
# child 1
class Person < BaseModel
attribute :name
end
# child 2
class Post < BaseModel
attribute :title
end
#usage
person = Person.create(name: "Salvatore Sanfillipo")
post = Post.create(title: "On cryptography and dogmas")
# blows up! with exeption:
Person.find(:uid => "c842563d-2f03-470b-91fd-379e668a9b8f")
# => Ohm::IndexNotFound: Ohm::IndexNotFound
# Work around
class Person < BaseModel
attribute :name
index :uid
end
class Post < BaseModel
attribute :title
index :uid
end
# With this work around the index is created.
# Note: be sure to flush your keys and delete models created.
person = Person.create(name: "Salvatore Sanfillipo")
post = Post.create(title: "On cryptography and dogmas")
Person.find(uid: "created-uid").size > 0
Place.find(uid: "created-uid-place").size >0