Module: DatastaxRails::FinderMethods

Included in:
Base, Relation
Defined in:
lib/datastax_rails/relation/finder_methods.rb

Overview

Relation methods to locate a single record either by PK or set of values.

Instance Method Summary collapse

Instance Method Details

#find(*args) ⇒ Object

Find operates with four different retrieval approaches:

  • Find by id - This can either be a specific id (1), a list of ids (1, 5, 6), or an array of ids ([5, 6, 10]). If no record can be found for all of the listed ids, then RecordNotFound will be raised.

  • Find first - This will return the first record matched by the options used. These options can either be specific conditions or merely an order. If no record can be matched, nil is returned. Use Model.find(:first, *args) or its shortcut Model.first(*args).

  • Find last - This will return the last record matched by the options used. These options can either be specific conditions or merely an order. If no record can be matched, nil is returned. Use Model.find(:last, *args) or its shortcut Model.last(*args).

All approaches accept an options hash as their last parameter.

Options

  • :conditions - See conditions in the intro.

  • :order - An SQL fragment like “created_at DESC, name”.

  • :limit - An integer determining the limit on the number of rows that should be returned.

  • :offset - An integer determining the offset from where the rows should be fetched. So at 5, it would skip rows 0 through 4.

Examples

# find by id

Person.find(1) # returns the object for ID = 1
Person.find(1, 2, 6) # returns an array for objects with IDs in (1, 2, 6)
Person.find([7, 17]) # returns an array for objects with IDs in (7, 17)
Person.find([1]) # returns an array for the object with ID = 1
Person.where(:administrator => 1).order(:created_on => :desc).find(1)

Note that the returned order is undefined unless you give a specific :order clause. Further note that order is handled in memory and so does suffer a performance penalty.

Examples

# find first
Person.first # returns the first object fetched by SELECT * FROM people
Person.where(:user_name => user_name).first
Person.order(:created_on => :desc).offset(5).first

# find last
Person.last # returns the last object in the column family
Person.where(:user_name => user_name).last
Person.order(:created_at => :desc).offset(5).last


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/datastax_rails/relation/finder_methods.rb', line 48

def find(*args)
  return to_a.find { |*block_args| yield(*block_args) } if block_given?

  options = args.extract_options!
  if options.present?
    apply_finder_options(options).find(*args)
  else
    case args.first
    when :first, :last
      send(args.first)
    else
      self.use_solr_value = false
      find_with_ids(*args)
    end
  end
end

#find_by(*args) ⇒ Object

Finds the first record matching the specified conditions. There is no implied ordering so if order matters, you should specify it yourself.

If no record is found, returns nil.

Post.find_by name: ‘Spartacus’, rating: 4 Post.find_by “published_at < ?”, 2.weeks.ago



73
74
75
76
# File 'lib/datastax_rails/relation/finder_methods.rb', line 73

def find_by(*args)
  where_values << escape_attributes(args.first)
  dont_escape.first
end

#find_by!(*args) ⇒ Object

Like find_by, except that if no record is found, raises an DatastaxRails::RecordNotFound error.



80
81
82
83
# File 'lib/datastax_rails/relation/finder_methods.rb', line 80

def find_by!(*args)
  where_values << escape_attributes(args.first)
  dont_escape.first!
end

#first(*args) ⇒ Object

A convenience wrapper for find(:first, *args). You can pass in all the same arguments to this method as you can to find(:first).



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/datastax_rails/relation/finder_methods.rb', line 87

def first(*args)
  if args.any?
    if args.first.is_a?(Integer) || (loaded? && !args.first.is_a?(Hash))
      limit(*args).to_a
    else
      apply_finder_options(args.first).first
    end
  else
    find_first
  end
end

#first!Object

Same as first but raises DatastaxRails::RecordNotFound if no record is found. Note that first! accepts no arguments.



101
102
103
# File 'lib/datastax_rails/relation/finder_methods.rb', line 101

def first!
  first || fail(RecordNotFound)
end

#last(*args) ⇒ Object

A convenience wrapper for find(:last, *args). You can pass in all the same arguments to this method as you can to find(:last).



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/datastax_rails/relation/finder_methods.rb', line 107

def last(*args)
  if args.any?
    if args.first.is_a?(Integer) || (loaded? && !args.first.is_a?(Hash))
      if order_values.empty? && reorder_value.nil?
        order(id: :desc).limit(*args).reverse
      else
        to_a.last(*args)
      end
    else
      apply_finder_options(args.first).last
    end
  else
    find_last
  end
end

#last!Object

Same as last but raises DatastaxRails::RecordNotFound if no record is found. Note that last! accepts no arguments.



125
126
127
# File 'lib/datastax_rails/relation/finder_methods.rb', line 125

def last!
  last || fail(RecordNotFound)
end