property :id, Serial
#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
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)
end
def nexts
Post.first(:created_on.gt => @post_date)
end
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@post_private ||= post.private
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_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])
endAny ideas?
def prev
Post.first(:created_on.lt => @post_date, :order => [:created_on.desc])
end
def nexts
Post.first(:created_on.gt => @post_date, :order => [:created_on.asc])
end
be aware that timestamps are not unique, i.e. they usually have no
millis or nanos. just a thought whether using them to iterate over
them.
- Kristian
On Tue, Jan 22, 2013 at 11:16 PM, kristian <m.kri...@web.de> wrote:
> take last instead of first :)
>
>> Audit.last( :id.lt => 10 )
> => #<Audit @id=9
>
> - Kristian
>