#156 ✓resolved
Andreas Ronge

ActiveModel support for Relationships

Reported by Andreas Ronge | March 4th, 2011 @ 09:13 AM | in 1.1.0

Would be really cool to have ActiveModel support for relationships

class Role < Neo4j::Rails::Relationship
  property :updated_at
  property :bar
  index :bar
  validates_uniqueness_of :bar
end

class Person < Neo4j::Rails::Model
  has_n(:manage).to(Person).relationship(Role)
  validates_associated :manage  #same as in ActiveRecord
end

a = Person.create
b = Person.create
Role.create(Person.manage, a,b, :bar => 'foo')

# or 
a.manage << b 
a.manage_rels.first #=> a Role instance

# or 
kalle = a.manage.build(:name => 'Kalle')
kalle.save

# or 
role = a.manage_rels.build(:name => 'Kalle')
role.save

# or
role = a.manage_rels.create(:bar => 'foo')

Also just like ActiveRecord (http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassM...)

  • collection.delete(object, …) Removes one or more objects from the collection by removing their associations from the join table. This does not destroy the objects.

  • collection.empty? Returns true if there are no associated objects.

  • collection.size Returns the number of associated objects.

  • collection.find(id) Finds an associated object responding to the id and that meets the condition that it has to be associated with this object. Uses the same rules as ActiveRecord::Base.find. Does also allow a block just normal Enumerable#find

  • collection.build(attributes = {}) Returns a new object of the collection type that has been instantiated with attributes and linked to this object with a relationship but not saved.

  • collection.create(attributes = {}) Returns a new object of the collection type that has been instantiated with attributes and linked to this object with a relationship and saves it (if it passed the validation).

Comments and changes to this ticket

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

Neo4j.rb is a graph database framework for JRuby.

It provides:

* Mapping of ruby objects to nodes in networks rather than in tables.
* Storage of ruby object to a file system.
* Fast traversal of relationships between nodes in a hugh node space.
* Transaction with rollbacks support.
* Indexing and quering of ruby objects.
* Integration with Rails

People watching this ticket

Referenced by

Pages