2012-04-02 3 views
0

기본 포럼을 구성했습니다. 게시물 # index 조치가 parent_post_id가 nil이 아닌 레코드 만 표시하도록하고 응답 게시물이 아닌 레코드를 표시하도록합니다. 나는 삐걱 소리를 내었습니다. 그러나 제대로 설정했는지 확신 할 수 없습니다.ActiveRecord, 부모 개체가없는 개체 만

#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 

답변

1

우리는 "게시물" "포스트"가 아니라 테이블의 이름이 "게시물"이기 때문에 말할

조건 섹션 http://api.rubyonrails.org/classes/ActiveRecord/Base.html

의 참조

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

시도

이 단순한 where 절에는 squeel이 필요하지 않습니다. "== nil"이 아닌 nil 값을 찾기위한 적절한 SQL 구문이므로 "null"이라고합니다.

+0

불행히도 이것은 작동하지 않는 것 같습니다. 'Post Load '(0.1ms) SELECT "posts". * FROM "posts"where "posts". "forum_id"= 1'은 여전히 ​​콘솔에서 처리되고 있으며 모든 게시물이 통과됩니다. 레벨 것들. – DVG

+0

레일즈 콘솔 자체에 문장을 입력 해보십시오. 어떻게 든 where 절을 추가하지 않았습니다. –

+0

그래, 내가보기 레이어에서 망쳐 놨어 – DVG

관련 문제