Add some ordering to the posts, this should return the correct records:

On 22 January 2013 17:13, Alex <[email protected]> wrote:

> Thanks guys, the code works, however there is a problem. The .gt works
> fine, but the .lt always gives me the first element in the db, not the
> previous element! I tried it with both the :id (@post_id) and the
> :created_on (@post_date) properties, getting the same result:
>
>   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
>>         @post_private ||= post.private
>>         @post_id ||= post.id
>>         @previous = prev.title
>>         @next = nexts.title
>>       end
>>     end
>>   end
>>   def prev
>>     Post.first(:created_on.lt => @post_date, :order => [:post_date.asc])
>>   end
>>   def nexts
>>     Post.first(:created_on.gt => @post_date, :order => [:post_date.asc])
>>   end
>
>
> Any ideas?
>

Again, I have to add this is untested gmail code ;)

Cheers,
Ben

-- 
You received this message because you are subscribed to the Google Groups 
"DataMapper" 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/datamapper?hl=en.

Reply via email to