On Jun 26, 2010, at 12:17 PM, storitel wrote:
hi folks,
another in my "surely there's a simple way to do that" series...
i have a database of Companies, and each company has Contacts. I'd
like to have PREV and NEXT buttons on the show page for each company
and each contact, so that users can traverse the list easily. is there
a standard trick?
You'd probably want to define, say, "previous_record" and
"next_record" on your models, then the links could look like:
<a to="previous_record">Prev</a>
etc. How to define them depends on your sorting. At the maximally-
configurable end, you could use acts_as_list (hobo_model, etc omitted):
class Company
acts_as_list
set_default_order 'position ASC'
def previous_record
higher_item
end
def next_record
lower_item
end
end
class Contact
acts_as_list :scope => :company
set_default_order 'position ASC'
...etc...
end
Or, if you've got an order defined by SQL, you can omit acts_as_list
entirely:
class Company
named_scope :next_records, lambda { |r| { :conditions => ['name
>= ? AND id > ?', r.name, r.id], :order => 'name ASC' } }
named_scope :prev_records, lambda { |r| { :conditions => ['name
<= ? AND id < ?', r.name, r.id], :order => 'name DESC' } }
def next_record
Company.next_records(self).first
end
def previous_record
Company.prev_records(self).first
end
end
and similarly for Contact, but with 'company.contacts' instead of
'Company' in the method definitions. I've guessed at the relevant
fields above, and the bits referring to 'id' are to provide a
consistent ordering of records with duplicate names; your application
may not have to worry about that case.
--Matt Jones
--
You received this message because you are subscribed to the Google Groups "Hobo
Users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/hobousers?hl=en.