Mongoid 7.0 single table inheritance with has_many - mongoid

I have a class A
class A
include Mongoid::Document
has_many :bs
accepts_nested_attributes_for :bs
and a class B
class B
include Mongoid::Document
belongs_to :a
and a class C that inherits from B
class C < B
field :new_field, type: String
This worked fine with Mongoid 6. With Mongoid 7, on a form with fields_for, upon submit, I now get:
Attempted to set a value for 'new_field' which is not allowed on the model B
Note, this is NOT the mongoid polymorphism supported in 7.0 (I believe) b/c that is not talking about single table inheritance (STI), rather, it supports multiple tables belonging to a single class / table as the same symbol. This is not that. And I've tried using as and polymorphic:true.
Any ideas how to fix this?
Thanks,
Kevin

Turns you for Mongoid 7.0 or Rails 5.2 (not sure which change broke this) you have to set the type in the form for STI, I did this by a hidden field:
<%f.hidden_field :_type, value: "C"%>
This allows you to set descendant-only attributes.

I faced a similar sort of issue. I was upgrading mongoid from 3.0 to 7.0 in a project and STI was implemented in the old app. _type was explicitly declared in the parent table which was causing unexpected behavior. I removed it and the project started working.

Related

Django OneToOne reverse relationship does not allow null values

I have this architecture (very simplified)
from django.db import Models
class MainClass(models.Model):
a = models.IntegerField()
b = models.CharField()
class OtherClass(models.Model):
c = models.IntegerField()
main = models.OneToOneField(MainClass, primary_key=True)
Which means my MainClass object has an attribute named otherclass, because of the existence of the reverse relationship between these models.
My problem is if I specify valid values for MainClass.a and MainClass.b, but None for MainClass.otherclass. I get the error
ValueError: Cannot assign None: "MainClass.otherclass" does not allow null values.
I understand there cannot be OtherClass without MainClass (it doesn't make sense), but why the opposite situation is also causing an error? Other way: Why cannot be MainClass without OtherClass?
Looks like this is a normal behaviour in Django 1.8, although the restriction has been removed in Django 1.10
So, This isn't an error.

getting all types for instance using owl api 3.4.10 and jfact 1.2.1

I used Protege to make an ontology. It consists of two base classes. Each of them has three subsclasses. Subclasses are classes with specific object and data properties. When I make an instance I make it as class individual but with such data properties so that instance also belongs to some subclass. When I run Hermit in Protege, it is well done. It says that my instance belongs to class and subclass. But in Jfact in Java and Owlapi, when I try to print all instances from subclasses, my instance belongs only to superclass, not to subclass. Is it about JFact or something else?
for (OWLClass c : ont.getClassesInSignature()) {
if (c.getIRI().getFragment().equals("Baby")) {
NodeSet<OWLClass> subClasses = reasoner.getSubClasses(c, false);
for (OWLClass subClass : subClasses.getFlattened()) {
System.out.println(subClass.getIRI().getFragment());
NodeSet<OWLNamedIndividual> instances = reasoner.getInstances(subClass, false);
for (OWLNamedIndividual i : instances.getFlattened()) {
System.out.println(i.getIRI().getFragment());
s += i.getIRI().getFragment() + " ";
}
}
}
}
Looks like a bug in JFact. Can you add an issue on its tracker, with version and ontology?
Edit: JFact 1.2.1 is ancient and has known bugs. Update to at least 1.2.4 (and OWLAPI 3.5.5, the most recent release for the 3.x version).
If possible, I recommend updating to OWLAPI 4 (for Java 7) or 5 (for Java 8) and the same main version for JFact.

Undefined Property: Security::$table [duplicate]

Hey I have coded CakePHP for a number of things but never ran into this problem before surprisingly. Also I have thoroughly searched the net and CakePHP docs and have not found an answer to my question. My question is, I have a table for my model that should be named Class, obviously I cannot use that name though since it's a reserved PHP keyword. What options do I have to be able to refer to this model appropriately.
So far I have;
Renamed my class model file to player_class.php
Renamed my class model class to PlayerClass
Changed var $name to 'PlayerClass'
Added to my class model class; var $useTable = 'classes';
Renamed my class controller to player_classes_controller.php
Renamed my class controller class to PlayerClassesController
Changed var $name to 'PlayerClasses'
While this does work, is this what has to be done or are to other options to be able to refer to it as Class still, like can I do any sort of mangling like _Class?
I once tested all CakePHP class names for Cake 1.2 if they can be used as Model names, here are the results:
NOT possible is:
app
appcontroller
appmodel
behaviorcollection
cache
cacheengine
cakelog
cakesession
classregistry
component
configure
connectionmanager
controller
datasource
debugger
dispatcher
file
fileengine
folder
helper
inflector
model
modelbehavior
object
overloadable
overloadable2
router
security
sessioncomponent
set
string
validation
Possible is:
acl
aclbase
aclbehavior
aclcomponent
aclnode
aclshell
aco
acoaction
admin
ajaxhelper
apcengine
apishell
app_model
apphelper
aro
authcomponent
bake
baker
bakeshell
behavior
cachehelper
cake
cakeschema
cakesocket
consoleshell
containablebehavior
controllertask
cookiecomponent
dbacl
dbaclschema
dbconfigtask
dboadodb
dbodb2
dbofirebird
dbomssql
dbomysql
dbomysqlbase
dbomysqli
dboodbc
dbooracle
dbopostgres
dbosource
dbosqlite
dbosybase
element
emailcomponent
error
errorhandler
extracttask
flay
formhelper
htmlhelper
httpsocket
i18n
i18nmodel
i18nschema
i18nshell
iniacl
javascripthelper
jshelper
jshelperobject
l10n
layout
magicdb
magicfileresource
mediaview
memcacheengine
modeltask
multibyte
numberhelper
page
pagescontroller
paginatorhelper
permission
plugintask
projecttask
requesthandlercomponent
rsshelper
sanitize
scaffold
schema
schemashell
securitycomponent
sessionhelper
sessionsschema
shell
shelldispatcher
test
testsuiteshell
testtask
texthelper
themeview
timehelper
translate
translatebehavior
treebehavior
viewtask
xcacheengine
xml
xmlelement
xmlhelper
xmlmanager
xmlnode
xmltextnode
When i run into this sort of problem i usually do what you did, only i prefix the reserved word with "My" (so when i read the code it doesn't look like that class has anything to do with "Player"... for example, just the other day i wanted to model a "ACO" model.. but that already existed in cake (same scenario of reserved word) so i created a model called Myaco.
I think you should just name it Myclass.
Regarding the model name and controller name changes- i think you did good, i would do the same. Your only real option is to use the $useTable = 'classed'; to use your DB table.
If you use the underscore prefix, i believe cake will not be able to handle it (it will fail in the Inflector class).
Good luck
I can second that solution. I had the same problem and used a prefix that was the initials of the client. Ended up calling mine Dtclass. Unfortunately, it took me an hour or so to figure out what the problem was. One of those cases where the answer stares you in the face all the time till you finally recognize it.

How to ignore an unknown subclass with single collection inheritance in Mongoid?

The default inheritance method for Mongoid is creating a single collection for all subclasses. For instance:
class User
include Mongoid::Document
end
class Admin < User
end
class Guest < User
end
Internally, Mongoid adds a _type field to each document with the class name, which is used to automatically map each instance to the right class.
The problem is, if I have a document in this collection with an unknown _type value, I get an exception:
NameError: uninitialized constant UnknownClass
This can happen if you create a new subclass of User, in the example above, and a migration that creates a new instance of this new subclass. Until you restart your servers, every query to this collection (like User.all.to_a). Is there a safe way to avoid this error?
The only solution I came up is rescuing NameError exception and querying by all known subclasses:
class User
def self.some_query(params)
self.where(params).to_a
rescue NameError => e
Rails.logger.error "Unknown subclass: #{e.message}"
subtypes = self.descendants.map(&:to_s)
self.where(params.merge(:_type.in => subtypes)).to_a
end
end

declarative authorization and mongoid embedded documents

I'm using declarative authorization and mongoid. I have association as
Class Company
embeds_many :divisions
end
Class Division
embedded_in :company
end
and route as
resources :companies do
resources :divisions
end
I added filter_resource_access in my divisions_controller.rb. Permission for division index is ok, but while editing and updating declarative authorization query with division id raising error Document not found for class Division with id(s) 13244adsf32gag3.
How would I make it work with embedded controller and documents too?

Resources