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
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".