ruby - Why does rails console give different size of relationship than in the server? -


my relationship between posts , comments showing differently in rails console , web server! how be? confused because partial rendering wrong links, , thought else wrong, partial should not have rendered @ because collection should empty! use if/else check size, , still showing partial empty relationship!

rails console:

irb(main):033:0> p=post.find(6) => #<post id: 6, title: "yahoo", comment: "the home page.", link: nil, user_id: nil, created_at: "2013-10-06 21:53:24", updated_at: "2013-10-07 00:43:25"> irb(main):034:0> p.comments.size => 0 

posts/show.haml:

%h2 comments post id: =@post.id , comment size:  =@post.comments.size - if @post.comments.empty?   no comments. - else   = render @post.comments 

browser: http://127.0.0.1:3000/posts/6

comments  post id: 6 , comment size: 1 commenter: 

comments/_comment.haml: doesn't seem relevant...

rails 4.0.0, ruby 2.0.0p247 (2013-06-27) [i386-mingw32]

maybe there form new comment somewhere on page , build new comment in controller:

@new_comment = @post.comments.build 

that's why @post.comments.count 1. rewrite code:

= @post.comments.reject{ |t| t.new_record? }.count 

upd.

there nicer way thing: instead of adding reject method add scope in comment model:

scope :saved, where('id not ?', nil) 

and in view:

@post.comments.saved.count 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -