Ruby on rails model method is not getting executed in controller

posts_controller.rb destroy method

def destroy

    if !request.xhr?

    if user_signed_in?

        if Post.exists?(:post_id => params[:id])

                if Post.post_is_mine(params[:id],

                    @return = { :error => false, :response => "Post deleted" }

                    @return = { :error => true, :response => 'You are not allowed to perform this action.' }

                @return = { :error => true, :response => 'This post doesn\'t exist.' }

            @return = { :error => true, :response => 'Please login before you delete a post.' }

    render :json => ActiveSupport::JSON.encode( @return )



  def self.post_is_mine(post_id, user_id)
    #where(:user_id => user_id, :post_id => bucket_id)
    where("user_id = ? and post_id = ?", user_id, bucket_id)

when i check what queries are being run when i destroy a post, i can only see the .exists? to run but not the .post_is_mine which simply goes through as it returns TRUE

i tried several other names as the method since something could cause the problem or even simply trying out the if statement with .post_is_mine but still, the query was not run

Could there be a problem on the model as to how i use the where clause?


The "where" will return an empty collection, which evaluates as truthy. You would need to add a check to see if there are any records in it to get the right true/false.

def self.post_is_mine(post_id, user_id)
  where("user_id = ? and post_id = ?", user_id, bucket_id).any?

Yes. #where returns an ActiveRecord Relation, which is used to generate your query. The relation will not be evaluated in your code, so the query from .post_is_mine will never be executed. if Post.postis mine(params[:id], returns true because the Relation object is not nil.

What you really want is to use exists? in the post_is_mine method.

def self.post_is_mine(post_id, user_id)
  exists?(:user_id => user_id, :post_id => bucket_id)


I was curious about the difference between my answer and Pavling's answer. For anyone else wondering:

#exists? executes a SQL statement with SELECT 1 FROM ...

#any? executes a SQL statement with SELECT COUNT(*) FROM ...

In practice there is likely not much difference between the two, but some crude benchmarks indicated that #any? is faster (with AR 3.2.6 and Postgresql 9.1 on OSX)

Need Your Help

How to shake on android 2.2

android shake

Does anybody know what is the shake code for android 2.2? I want on shake to make buzz on my application. Thanks.

C# How do you calculate number of days using Month Calendar in Windows Forms?

c# winforms visual-studio-2010 calendar timespan

I'm using Visual C# 2010 Express to create a Form that has the user choose a type of car(compact, standard, luxury) and a date on the calendar for how long they are going to rent it. I can't seem to

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.