Forum: Ruby on Rails Returning Count from find_by_sql

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
C4bd59695df68a428fc2f7a165d59a19?d=identicon&s=25 Will Merydith (wmerydith)
on 2006-04-20 02:50
I want to return an int from a find_by_sql call.  But when I use that
result to compare to another count the error tells me I have an array.

MODEL:

    # Returns the number of comments in a specified post
    def self.find_count(post_id)
        find_by_sql(["SELECT COUNT(*) FROM commments WHERE post_id = ?",
                    post_id])
    end

CONTROLLER:

  def test
    @post = Post.find(@params["post_id"])
    @comment =
      Comment.find_next(@params["post_id"], @params["position"])
    @count = Comment.find_count(@params["post_id"])
  end

VIEW:

line 1: <% if @comment.position < @count -%>

....

ERROR:

ActionView::TemplateError (comparison of Fixnum with Array failed) on
line #1 of app/views/main/_test.rhtml:


Do I have a syntax error?
C64e63b70be7dfed8b0742540b8b27e5?d=identicon&s=25 Mark Reginald James (Guest)
on 2006-04-20 05:54
(Received via mailing list)
Will Merydith wrote:
> I want to return an int from a find_by_sql call.  But when I use that
> result to compare to another count the error tells me I have an array.

Use count_by_sql instead.

--
We develop, watch us RoR, in numbers too big to ignore.
This topic is locked and can not be replied to.