On 22 January 2013 12:04, Alex <a...@sicanstudios.com> wrote:

> Thanks Ben. I do store timestamps in the table. Would you suggest I should
> check for older/newer timestamps inside this loop?
>
>   #defines if a post of a user exists in db
>>   def post_in_db
>>     posts_of_user.each do |post|
>>       if post.slug == params[:post_slug]
>>         @post_slug ||= post.slug
>>         @post_title ||= post.title
>>         @post_body ||= post.body
>>         @post_date ||= post.created_on
>>       end
>>     end
>>     if @post_body == nil
>>       redirect ('/' + params[:nickname])
>>     end
>>   end
>
>
> or I should store the @post_date and create a new helper with a new loop
> to find the immediate older/newer timestamp before/after the @post_date?
> I’m thinking in terms of performance the first method would be better.
>
> I'd just do a check for created_on < current for previous and created_on >
current for next and just call `first` with the predicate.

For example:

class Post
  def prev
    first(:created_at.lt => self.created_at)
  end

  def next
    first(:created_at.gt => self.created_at)
  end

  #...
end

Note: This is gmail code and unchecked/tested :)

Cheers,
Ben

-- 
You received this message because you are subscribed to the Google Groups 
"DataMapper" group.
To post to this group, send email to datamapper@googlegroups.com.
To unsubscribe from this group, send email to 
datamapper+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/datamapper?hl=en.

Reply via email to