Hi there,
I've decided that instead of deleting all posts on a user's profile,
there is now an active_post column in the posts table.
Basically, a user has only 1 active post at a time, and the database
is either a value of 0 (inactive) or 1(active)
I have a problem though. When i add a post, I see that the active
column is set to 1.
When I add a 2nd post, the most recent is set to active_post = 1 and
the initial post is set to 0. (this is as i expect).
However - if I delete a post using the link on the page, the database
value sets to 0 (also what I want and expect)
But - the last post is still displayed on the page.
Below is the code on the posts controller, that hopefully explains
what I'm doing:
Basically - does anyone know how to hide this post? (as it stands the
posts for this user are inactive, so should not be visible to them)
I think it's to do with the :conditions.
def index
#@posts = Post.find_by_user_id( session[:user_id], :order =>
"created_at DESC")
Post.find_by_user_id( session[:user_id], 1, :limit =>
1, :conditions => "active_post", :order => "created_at desc")
@posts = fetchFirstPostForSessionUser()
respond_to do |format|
format.html
end
end
def create
markAsInactive( fetchFirstPostForSessionUser );
@post = Post.create(:message => params[:message], :user_id =>
session[:user_id])
respond_to do |format|
if @post.save
format.html { redirect_to :controller => "profile"}
flash[:notice] = "Message saved."
format.js
else
flash[:notice] = "Message failed to save."
format.html { redirect_to :controller => "profile" }
end
end
end
def delete
#@post =
markAsInactive( Post.find_by_user_id( session[:user_id], 1, :limit
=> 1, :conditions => "active_post = 1", :order => "created_at desc"))
respond_to do |format|
format.html { redirect_to :controller => "user"}
#format.xml { head :ok }
end
end
private
def markAsInactive( post )
@post = post
if not @post.nil?
@post.active_post = 0;
@post.save
end
end
def fetchFirstPostForSessionUser
# TODO Fixme - only fetch active posts
Post.find_by_user_id( session[:user_id], 1, :limit =>
1, :conditions => "active_post = 1", :order => "created_at desc")
#Post.find(:all, :conditions => :active_post = '1', :order =>
"created_at DESC")
end
Many Thanks