0

I've got a basic forum set up. I want the posts#index action to only show records where parent_post_id is nil, and therefore not a reply post. I've got squeel installed, but I'm not sure if I have it set up right.

  #app/controllers/posts_controller.rb
  def index
    @forum = Forum.find(params[:forum_id])
    @posts = @forum.posts.where{ |post| post.thread == nil }
  end

 #app/models/post.rb
class Post < ActiveRecord::Base
  has_many :replies, :class_name => "Post"
  belongs_to :thread, :class_name => "Post", :foreign_key => "parent_post_id"
end

#config/initializers/squeel.rb
Squeel.configure do |config|
  # To load hash extensions (to allow for AND (&), OR (|), and NOT (-) against
  # hashes of conditions)
  config.load_core_extensions :hash

  # To load symbol extensions (for a subset of the old MetaWhere functionality,
  # via ARel predicate methods on Symbols: :name.matches, etc)
  config.load_core_extensions :symbol

  # To load both hash and symbol extensions
  config.load_core_extensions :hash, :symbol
end
DVG
  • 17,392
  • 7
  • 61
  • 88

1 Answers1

1

Try

@posts = @forum.posts.where{ "posts.parent_post_id is null" }

See

Conditions section of http://api.rubyonrails.org/classes/ActiveRecord/Base.html

We say "posts" not "post" since the name of the table is "posts"

Added You don't need squeel for this type of simple where clause. We say "is null" since that is the proper SQL syntax for finding nil values, not "== nil".

Larry K
  • 47,808
  • 15
  • 87
  • 140
  • Unfortunately, this doesn't seem to be working. `Post Load (0.1ms) SELECT "posts".* FROM "posts" WHERE "posts"."forum_id" = 1` is still what's coming through in the console, and all the posts are coming through, not just the top-level ones. – DVG Apr 02 '12 at 11:52
  • Try entering the statements in the rails console itself. You're missing the added where clause somehow. – Larry K Apr 02 '12 at 16:56
  • Yeah, it turns out I was messing it up in the view layer – DVG Apr 03 '12 at 21:26