Module: Neo4j::Rails::Finders::ClassMethods

Defined in:
lib/neo4j/rails/finders.rb

Overview

Defines the ##find method. When declaring properties with index a number of finder methods will be generated, similar to active record, example find_by_<property_name>, +find_or_create_by_<property_name>. all_by_<property_name>

Examples:

find_or_create_by

class Person < Neo4j::Rails::Model
  property :age, :type => Fixnum
end

Person.find_by_age(42)
Person.find_or_create_by
Person.find_or_create_by!(:age => 'bla')

find all

Person.all_by_age
Person.all(:name => 'bla')
Person.all('name: "bla"')  # lucene query syntax

See Also:

Instance Method Summary (collapse)

Instance Method Details

- (Object) all(*args, &block)



168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/neo4j/rails/finders.rb', line 168

def all(*args, &block)
  if !conditions_in?(*args)
    # use the _all rule to recover all the stored instances of this node
    _all
  else
    # handle the special case of a search by id
    ids = ids_in(args.first)
    if ids
      [find_with_ids(ids)].flatten
    else
      find_with_indexer_or_traversal(*args, &block)
    end
  end
end

- (Object) close_lucene_connections

Call this method if you are using Neo4j::Rails::Model outside rails This method is automatically called by rails to close all lucene connections.



204
205
206
207
# File 'lib/neo4j/rails/finders.rb', line 204

def close_lucene_connections
  Thread.current[:neo4j_lucene_connection].each {|hits| hits.close} if Thread.current[:neo4j_lucene_connection]
  Thread.current[:neo4j_lucene_connection] = nil
end

- (Object) count



198
199
200
# File 'lib/neo4j/rails/finders.rb', line 198

def count
  all.size
end

- (Object) find(*args, &block)

Behave like the ActiveRecord query interface

Example

Handle Model.find(params[:id])
Model.find
Model.find(:first)
Model.find("1")
Model.find(1)
Model.find("name: test")
Model.find(:name => "test")
Model.find(:first, "name: test")
Model.find(:first, { :name => "test" })
Model.find(:first, :conditions => "name: test")
Model.find(:first, :conditions => { :name => "test" })
Model.find(:all, "name: test")
Model.find(:all, { :name => "test" })
Model.find(:all, :conditions => "name: test")
Model.find(:all, :conditions => { :name => "test" })


112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/neo4j/rails/finders.rb', line 112

def find(*args, &block)
  case args.first
    when :all, :first
      kind = args.shift
      send(kind, *args, &block)
    when "0", 0, nil
      nil
    else
      if convertable_to_id?(args.first)
        find_with_ids(*args)
      else
        first(*args, &block)
      end
  end
end

- (Object) find!(*args)

Finds a model by given id or matching given criteria. When node not found, raises RecordNotFoundError



130
131
132
133
134
# File 'lib/neo4j/rails/finders.rb', line 130

def find!(*args)
  self.find(*args).tap do |result|
    raise Neo4j::Rails::RecordNotFoundError if result.nil?
  end
end

- (Node) find_or_create_by(attrs = {}, &block)

Find the first Node given the conditions, or creates a new node with the conditions that were supplied.

Examples:

Find or create the node.

Person.find_or_create_by(:name => "test")

Parameters:

  • attrs (Hash) (defaults to: {})

    The attributes to check.

Returns:

  • (Node)

    A matching or newly created node.



145
146
147
# File 'lib/neo4j/rails/finders.rb', line 145

def find_or_create_by(attrs = {}, &block)
  find_or(:create, attrs, &block)
end

- (Object) find_or_create_by!(attrs = {}, &block)

Similar to find_or_create_by,calls create! instead of create Raises RecordInvalidError if model is invalid.



151
152
153
# File 'lib/neo4j/rails/finders.rb', line 151

def find_or_create_by!(attrs = {}, &block)
  find_or(:create!, attrs, &block)
end

- (Node) find_or_initialize_by(attrs = {}, &block)

Find the first Node given the conditions, or initializes a new node with the conditions that were supplied.

Examples:

Find or initialize the node.

Person.find_or_initialize_by(:name => "test")

Parameters:

  • attrs (Hash) (defaults to: {})

    The attributes to check.

Returns:

  • (Node)

    A matching or newly initialized node.



164
165
166
# File 'lib/neo4j/rails/finders.rb', line 164

def find_or_initialize_by(attrs = {}, &block)
  find_or(:new, attrs, &block)
end

- (Object) first(*args, &block)



183
184
185
186
187
188
189
190
191
# File 'lib/neo4j/rails/finders.rb', line 183

def first(*args, &block)
  found = all(*args, &block).first
  if found && args.first.is_a?(Hash) && args.first.include?(:id)
    # if search for an id then all the other properties must match
    args.first.find{|k,v| k != :id && found.send(k) != v} ? nil : found
  else
    found
  end
end

- (Object) last(*args)



193
194
195
196
# File 'lib/neo4j/rails/finders.rb', line 193

def last(*args)
  a = all(*args)
  a.empty? ? nil : a[all.size - 1]
end

- (Object) load(*ids)

load an id or array of ids from the database



81
82
83
84
85
86
87
88
# File 'lib/neo4j/rails/finders.rb', line 81

def load(*ids)
  result = ids.map { |id| load_entity(id) }
  if ids.length == 1
    result.first
  else
    result
  end
end