Module: LazyFind::ActiveRecord::FinderMethods

Defined in:
lib/lazy_find/active_record/finder_methods.rb

Constant Summary collapse

MAPPING =
{:first => 0, :last => -1}

Instance Method Summary collapse

Instance Method Details

#first(attr = nil) ⇒ Object

Find the first record (or first N records if a parameter is supplied). If no order is defined it will order by primary key.

Person.first # returns the first object fetched by SELECT * FROM people ORDER BY people.id LIMIT 1
Person.where(["user_name = ?", user_name]).first
Person.where(["user_name = :u", { u: user_name }]).first
Person.order("created_on DESC").offset(5).first
Person.first(:email => "jenorish@gmail") # returns the first three objects fetched by SELECT * FROM people WHERE email= '[email protected]'  ORDER BY people.id LIMIT 3


15
16
17
18
# File 'lib/lazy_find/active_record/finder_methods.rb', line 15

def first(attr = nil)
   return super(attr) if attr.blank? || attr.class == Fixnum 
   lazy_find(attr,:first)
end

#last(attr = nil) ⇒ Object



38
39
40
41
# File 'lib/lazy_find/active_record/finder_methods.rb', line 38

def last(attr = nil)
  return super(attr) if attr.blank? || attr.class == Fixnum 
  lazy_find(attr,:last)
end

#take(attr = nil) ⇒ Object

Gives a record (or N records if a parameter is supplied) without any implied order. The order will depend on the database implementation. If an order is supplied it will be respected.

Person.take # returns an object fetched by SELECT * FROM people LIMIT 1
Person.take({:email => "jenorish@gmail"}) # returns matched objects fetched by SELECT * FROM people WHERE email= '[email protected]' LIMIT 5
Person.where(["name LIKE '%?'", name]).take


51
52
53
54
# File 'lib/lazy_find/active_record/finder_methods.rb', line 51

def take(attr = nil)
  return super(attr) if attr.blank? || attr.class == Fixnum 
  lazy_find(attr,:take)
end