marking posts as 'Inactive'....

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

Can you explain what :conditions => "active_post" is supposed to do?
Possibly you should be comparing the value of active_post with some
value?

Colin

Hi Colin,

:conditions active_post is meant to only display posts that are
active.

If a user deletes a post (they think it's deleted, but infact it is
archived and just set as inactive) as I may want to do something with
it at a later date on the site.

I looked on the rails api site, I was therefore kinda hoping that :

  Post.find_by_user_id( session[:user_id], 1, :limit =>
1, :conditions => "active_post = 1", :order => "created_at desc")
would only return posts where the active flag is set to 1.

What i've done is maybe wrong, but i hope that explanation makes
sense....?

I would kinda hope that would happen too. What did happen?

Is active_post an integer type column or a string? If it is a string
then "active_post = '1'" might be better as integer 1 is not the same
as string '1'

Colin

active_post is set as 'tinyint'.

What is currently happening is that I hit 'delete'.

The active_post column is set to 0, but the post is still visible on
the front end.

In short - I only want the active post to be visible to the user. If
the delete it, it is archived, and no posts visible, until they add a
new one - so a user should only ever have 1 active post.

Hope this makes sense? Is there a way I can do this?

It is best not to top post, it is easier to follow the thread if
things are kept in order, I have reordered.

...

> I looked on the rails api site, I was therefore kinda hoping that :

> Post.find_by_user_id( session[:user_id], 1, :limit =>
> 1, :conditions => "active_post = 1", :order => "created_at desc")
> would only return posts where the active flag is set to 1.

I would kinda hope that would happen too. What did happen?

Is active_post an integer type column or a string? If it is a string
then "active_post = '1'" might be better as integer 1 is not the same
as string '1'

active_post is set as 'tinyint'.

What is currently happening is that I hit 'delete'....

What I meant was what does the the query
Post.find_by_user_id( session[:user_id], 1, :limit => 1, :conditions
=> "active_post = 1", :order => "created_at desc")
return?

If this returns a post with active_post not 1 then there is something
strange going on. Run it in a console window and see what it returns.
Also if you have not done so already work through the rails guide on
debugging. This will show you how using ruby-debug you can break in
(after that find for example) and see what it has returned.

I have just looked at your code again and realised that I was looking
at the line before your call to fetchFirstPostForSessionUser, which is
not commented out but of course you are ignoring the return value. I
see that in fetchFirst.. you are using the condition with = 1 so it
should be ok. As I said try debugging and see what it is returning.
Also you can look in development.log and see exactly the query being
executed.

Colin

I'll have a look around and see whats going on... Many Thanks

Colin,

Before I debug.... I have had an idea:

What about the view?

If I can write a decent if statement to only return active_posts that
= 1 I should be good to go. I did try this today, but it still showed
the inactive post when I set it to 0..

Here's what I wrote - There's maybe a better (correct way) to do it...

<% if @posts.active_post = 1 %>

<% render :partial => posts %>

<% end %>

<% if @posts.active_post != 1 %>

There are no posts!

<% end %>

Sadly this didn’t work, but I wondered if anyone knew why? I can
delete a post, setting the 1 value to a 0, but the post still remains
in the view…

Hmm.

Colin,

Before I debug.... I have had an idea:

What about the view?

If I can write a decent if statement to only return active_posts that
= 1 I should be good to go. I did try this today, but it still showed
the inactive post when I set it to 0..

Here's what I wrote - There's maybe a better (correct way) to do it...

<% if @posts.active_post = 1 %>

That should be == 1

<% render :partial => posts %>

<% end %>

<% if @posts.active_post != 1 %>

You can use <% else %> rather then end and another if

There are no posts!

<% end %>

Sadly this didn’t work, but I wondered if anyone knew why? I can
delete a post, setting the 1 value to a 0, but the post still remains
in the view…

If still not working put in your view some debug:
<p>posts active_post value is <%= @posts.active_post %></p>

But you would be better off debugging the controller first to check
that the query is working correctly. Did you look in development.log
to see exactly what query is being run?

Colin

cool - i'll give this a go. I hope it's as simple as == instead of =.

Dev log showed my query as a select statement from posts, not
specifically looking at the active_post.

will try a bit more.... :slight_smile:

cheers

cool - i'll give this a go. I hope it's as simple as == instead of =.

Dev log showed my query as a select statement from posts, not
specifically looking at the active_post.

Are you saying that the :conditions => "active_post = 1" is not in the
actual query run? If so then that is the problem. Are you sure you
are looking at the right bit of code? I think you need to debug the
controller.

Colin

I'll need to take another look at it. I dont think it's there,

Would it look like select * from posts where active_post = 1?

I'll take a closer look later..

Cheers Colin

If it is using the code in fetchFirstPostForSessionUser that you
posted earlier, ie
Post.find_by_user_id( session[:user_id], 1, :limit => 1, :conditions
=> "active_post = 1", :order => "created_at desc")

Then it should have all of that in it - the user id the conditions,
the order and the limit.

If your log is getting large you can just delete it and then run the
query to make it easier to find the bits of interest.

Colin

By the way I do wish you would stop top posting, it makes it much
harder to follow what you are replying to.

Sorry about that - it this better?

UPDATE `posts` SET `updated_at` = '2009-12-09 20:24:55', `active_post`
= 0 WHERE `id` = 176

That looks correct in my opinion, and what I know about SQL.

So - I think the problem is with the view.

If the query is working correctly, then all i need to do is correctly
show a post (if it is active only)

So I wrote this:
    <% if @posts.active_post == 1 %>
    <%= render :partial => @posts %>
    <% end %>

This doesn't return a post - even though the database shows 1 active
post. (active_post = 1)

Debugging - I'm new to rails and find this hard enough to follow. I've
not learnt anything about debugging so wouldn't know where to start. I
think the issue is the view, but what do you think?

CHeers

> > I'll need to take another look at it. I dont think it's there,

> > Would it look like select * from posts where active_post = 1?

> If it is using the code in fetchFirstPostForSessionUser that you
> posted earlier, ie
> Post.find_by_user_id( session[:user_id], 1, :limit => 1, :conditions
> => "active_post = 1", :order => "created_at desc")

> Then it should have all of that in it - the user id the conditions,
> the order and the limit.

> If your log is getting large you can just delete it and then run the
> query to make it easier to find the bits of interest.

> Colin

> By the way I do wish you would stop top posting, it makes it much
> harder to follow what you are replying to.

Sorry about that - it this better?

From my log file I have this:

UPDATE `posts` SET `updated_at` = '2009-12-09 20:24:55', `active_post`
= 0 WHERE `id` = 176

That looks correct in my opinion, and what I know about SQL.

So - I think the problem is with the view.

If the query is working correctly, then all i need to do is correctly
show a post (if it is active only)

So I wrote this:
<% if @posts.active_post == 1 %>
<%= render :partial => @posts %>
<% end %>

This doesn't return a post - even though the database shows 1 active
post. (active_post = 1)

Debugging - I'm new to rails and find this hard enough to follow. I've
not learnt anything about debugging so wouldn't know where to start. I
think the issue is the view, but what do you think?

CHeers

Hi Colin,

You'll be pleased (relieved) to know I seem to have resoled it.

It was the view after all! I changed :

                <% if @posts.active_post == 1 %>
                <%= render :partial => @posts %>

TO:
      <% if @posts.active_post? %>
      <%= render :partial => @posts %>

This now only renders a post when it is active.
When I delete the post, the view displays no posts. Voila. :slight_smile:

Thanks so much for the posts on this. Perseverance prevailed!!!

Regards

On Wed, Dec 9, 2009 at 5:46 PM, RubyonRails_newbie <

> > > > I'll need to take another look at it. I dont think it's there,

> > > > Would it look like select * from posts where active_post = 1?

> > > If it is using the code in fetchFirstPostForSessionUser that you
> > > posted earlier, ie
> > > Post.find_by_user_id( session[:user_id], 1, :limit => 1, :conditions
> > > => "active_post = 1", :order => "created_at desc")

> > > Then it should have all of that in it - the user id the conditions,
> > > the order and the limit.

> > > If your log is getting large you can just delete it and then run the
> > > query to make it easier to find the bits of interest.

> > > Colin

> > > By the way I do wish you would stop top posting, it makes it much
> > > harder to follow what you are replying to.

> > Sorry about that - it this better?

> > From my log file I have this:

> > UPDATE `posts` SET `updated_at` = '2009-12-09 20:24:55', `active_post`
> > = 0 WHERE `id` = 176

> > That looks correct in my opinion, and what I know about SQL.

> > So - I think the problem is with the view.

> > If the query is working correctly, then all i need to do is correctly
> > show a post (if it is active only)

> > So I wrote this:
> > <% if @posts.active_post == 1 %>
> > <%= render :partial => @posts %>
> > <% end %>

> > This doesn't return a post - even though the database shows 1 active
> > post. (active_post = 1)

> > Debugging - I'm new to rails and find this hard enough to follow. I've
> > not learnt anything about debugging so wouldn't know where to start. I
> > think the issue is the view, but what do you think?

> > CHeers

> Hi Colin,

> You'll be pleased (relieved) to know I seem to have resoled it.

> It was the view after all! I changed :
> > <% if @posts.active_post == 1 %>
> > <%= render :partial => @posts %>

> TO:
> <% if @posts.active_post? %>
> <%= render :partial => @posts %>

> This now only renders a post when it is active.
> When I delete the post, the view displays no posts. Voila. :slight_smile:

> Thanks so much for the posts on this. Perseverance prevailed!!!

> Regards

> --

____________
Rodrigo Dellacqua wrotes:

Craig,

As I mentioned before, you shoudn't put that kind of behaviour in your
views.

That's a bad design and will lead to maintenance problems in the future.

What you should do is apply a ViewModel pattern to that, making this model
make the needed changes to your objects and them this ViewModel would be
passed in to the view.

But you shoudn't really send all posts to the view and filters on there, you
should only retrieve what you are going to use, if you keep that way, you
are creating a non-scalable app.

[]'s
Rodrigo Dellacqua- Hide quoted text -

- Show quoted text -

Hi All,

Although it may be bad design - i'm going to keep it as it is for the
first roll out of my app, and make changes once i've found my feet
with rails.

I need help with the following if statement:

> <% if @posts.active_post? %>
> <%= render :partial => @posts %>

This works perfectly if a user has a post.

However, If i create a new user (they have no posts)

When the user page is then loaded, I get an error regarding the
active_post. Obviously as the user is new, and has not posted
anything, they now get an error.

How can i refine the statement to allow the page to render, even in
the case of a new user?

Cheers

It has been suggested that you work through some of the Rails guides
and learn how to use the available debugging aids, which apparently
you do not have the inclination to do. It has also been pointed out
that the design you are using is probably not the best but you are
pressing on anyway. This is not the best attitude for encouraging
others to offer assistance.

Colin

I wasn't meaning to have a poor attitude. I was just keen/eager to get
my first version of the app out, giving me time to focus on
improvements.
My app is a seasonal one, so hence the reason for 'pressing on'.

I do want to learn the debugging, and am still reading endless posts/
books and tutorials along the way.