Model inheritance and relationships exception

22 views
Skip to first unread message

kichael

unread,
Sep 22, 2011, 7:18:21 AM9/22/11
to neo4jrb
Setting a relationship on a model that inherits from another model
causes an exception.

For example:

class BizProcess < Neo4j::Rails::Model
property :title, :type => String
end

class Innovation < BizProcess
has_n(:deps).to(Department)
end

class Department < Neo4j::Rails::Model
property :name, :type => String
index :name

has_n(:innovations).from(Innovation, :deps)
end

Then when typing 'Innovation' in rails console:
> Innovation
LoadError: Expected /app/models/innovation.rb to define Innovation

In the stack trace down the line it points to the issue being the line
in the Department model that defines the relationship.

When setting Innovation to inherit from Neo4j::Rails::Model, the
problem goes away, but that's not an acceptable solution.

One workaround I've used it defining it in the BizProcess model, but
this is confusing to others.

Is this by design or is this an issue?

Thanks,

Michael

Andreas Ronge

unread,
Sep 22, 2011, 7:35:20 AM9/22/11
to neo...@googlegroups.com
Hi

Interesting. Looks like an issue.
Maybe it is related to how rails is loading undefined constants/classes.

Can you try just using plain Ruby classes without any inheritance from
Neo4j::Rails::Model
and see if the same problem occurs ?

Something like this:

biz_process.rb:
class BizProcess
end

innovation.rb:
class Innovation < BizProcess
puts Department
end

department.rb:
class Department
end

> --
> You received this message because you are subscribed to the Google Groups "neo4jrb" group.
> To post to this group, send email to neo...@googlegroups.com.
> To unsubscribe from this group, send email to neo4jrb+u...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/neo4jrb?hl=en.
>
>

kichael

unread,
Sep 22, 2011, 7:55:25 AM9/22/11
to neo4jrb

When I changed the classes to how you specified:

> Innovation
Department
=> Innovation

Andreas Ronge

unread,
Sep 22, 2011, 8:12:31 AM9/22/11
to neo...@googlegroups.com

Andreas Ronge

unread,
Sep 22, 2011, 1:00:34 PM9/22/11
to neo...@googlegroups.com
Hi

I can't reproduce this bug and I'm therefore not sure it is a bug,

Here is what I did.

1. Create a new rails project
rails new qwe -m http://andreasronge.github.com/rails3.rb

2. Create the three model classes:
models/
|-- biz_process.rb
|-- department.rb
`-- innovation.rb

biz_process.rb:


class BizProcess < Neo4j::Rails::Model
property :title, :type => String
end

innovation.rb:


class Innovation < BizProcess
has_n(:deps).to(Department)
end

department.rb:


class Department < Neo4j::Rails::Model
property :name, :type => String
index :name
has_n(:innovations).from(Innovation, :deps)
end

4. Start the rails console: rails s

5. Type rails c
Loading development environment (Rails 3.1.0)
jruby-1.6.4 :001 > Innovation
=> Innovation
jruby-1.6.4 :002 > Innovation.create
Enable remote shell at port port=9332
Starting local Neo4j using db /home/andreas/tmp/qwe/db/neo4j-development
=> #<Innovation:0x3ee0b7 @errors=#<ActiveModel::Errors:0x1af431c
@base=#<Innovation:0x3ee0b7 ...>, @messages=#<OrderedHash {}>>,
@changed_attributes={}, @properties={"_classname"=>"Innovation"},
@_java_node=#<#<Class:0x1015fd4a3>:0x911b9f>, @relationships={},
@previously_changed={"_classname"=>[nil, "Innovation"]},
@validation_context=nil>

/Andreas

kichael

unread,
Sep 23, 2011, 12:36:08 AM9/23/11
to neo4jrb
After looking into it to reproduce the bug, one needs to add any
relationship definition to BizProcess.

So even adding a has_n(:departments).to(Department) to BizProcess
would suffice to reproduce. But any relationship to any model seems to
trigger it. Once I comment out all the relationships in BizProcess,
the issue goes away.

On Sep 22, 10:00 am, Andreas Ronge <andreas.ro...@gmail.com> wrote:
> Hi
>
> I can't reproduce this bug and I'm therefore not sure it is a bug,
>
> Here is what I did.
>
> 1. Create a new rails project
>   rails new qwe -mhttp://andreasronge.github.com/rails3.rb

Andreas Ronge

unread,
Sep 23, 2011, 10:49:42 AM9/23/11
to neo...@googlegroups.com
Ok, I have managed to reproduce this bug without using neo4j.rb with this:
class Department
puts "Department #{Innovation}"
end

class Innovation < BizProcess
puts "Innovation #{Department}"
end

class BizProcess
puts "BizProcess #{Department}"
end

I suggest that you in the innovation.rb file does a forward
declaration of BizProcess
innovation.rb:


class BizProcess < Neo4j::Rails::Model

end

class Innovation ...

biz_process.rb:
# open up the BizProcess again and add all your properties and has_n etc...

Is that a good enough work around ?

/Andreas

kichael

unread,
Sep 24, 2011, 4:39:49 PM9/24/11
to neo4jrb
That didn't work for me, as I could not access the various
relationships defined in BizProcess through an Innovation.

My files look like this with your suggested changes:

innovation.rb
class BizProcess < Neo4j::Rails::Model
end
class Innovation < BizProcess

has_n(:deps).to(Department)

end

department.rb
class Department < Neo4j::Rails::Model
property :name, :type => String

has_n(:biz_processes).from(BizProcess, :departments)
has_n(:innovations).from(Innovation, :deps)
end

biz_process.rb
class BizProcess < Neo4j::Rails::Model
property :title, :type => String
has_n(:departments).to(Department)
end

When I try to do innovation.departments, I get 'undefined method
`departments' for #<Innovation:0x4eb525a6>'

Is this what you meant by forward declaration or did I misread
somewhere?

Thanks,

Michael
Reply all
Reply to author
Forward
0 new messages