Witam, proszę o pomoc z kolejnym problemem. Otóż chciałbym żeby na moim forum pod każdym postem wyświetlała się liczba dodanych do niego komentarzy. Nie wiem jak odpowiednio zdefiniować zmienną która posłużyłaby mi w widoku index. Akcja komentowania działa bez zarzutu, odpowiednie komentarze wyświetlają się pod odpowiednim postem, ale co zrobić by je zliczyć? Wiem że w akcji show wystarczyłoby zrobić @post.comment.count, ale jak to zrobić w index skoro tam nie da się zainicjować @post = Post.find(params[:id])? Czy za pomocą instrukcji warunkowej która będzie porównywać post.id z comment.post_id? Pomóżcie proszę.
MODEL POST
class Post < ActiveRecord::Base
has_many :comment
end
MODEL COMMENT
class Comment < ActiveRecord::Base
belongs_to :post
end
MIGRACJA POSTÓW
class CreatePosts < ActiveRecord::Migration
def self.up
create_table :posts do |t|
t.column :title, :string
t.column :text, :text
t.column :author, :string
t.column :author_id, :integer
t.column :data, :datetime
end
end
def self.down
drop_table :posts
end
end
MIGRACJA KOMENTARZY
class CreateComments < ActiveRecord::Migration
def self.up
create_table :comments do |t|
t.column :text, :text
t.column :author, :string
t.column :author_id, :integer
t.column :post_id, :integer, :foreign_key => :posts
t.timestamps
end
end
def self.down
drop_table :comments
end
end
KONTROLER POSTÓW
class PostsController < ApplicationController
include ApplicationHelper
helper :profile
before_filter :protect, :only => [:index, :edit]
def index
@posts = Post.all
respond_to do |format|
format.html
format.xml { render :xml => @posts }
end
end
def show
@post = Post.find(params[:id])
@user = User.find(session[:user_id])
respond_to do |format|
format.html
format.xml { render :xml => @post }
end
end
def new
@post = Post.new
respond_to do |format|
format.html
format.xml { render :xml => @post }
end
end
def edit
@post = Post.find(params[:id])
end
def create
@post = Post.new(params[:post])
@post.data = Time.now
@user = User.find(session[:user_id])
@post.author = @user.screen_name
@post.author_id = @user.id
if @post.save
flash[:notice] = ‘Post został dodany’
redirect_to :action => ‘index’
else
render :action => ‘new’
end
end
def update
@post = Post.find(params[:id])
respond_to do |format|
if @post.update_attributes(params[:post])
format.html { redirect_to(@post, :notice => ‘Post został zaktualizowany’) }
format.xml { head :ok }
else
format.html { render :action => “edit” }
format.xml { render :xml => @post.errors, :status => :unprocessable_entity }
end
end
end
def destroy
@post = Post.find(params[:id])
@post.destroy
respond_to do |format|
format.html { redirect_to(posts_url) }
format.xml { head :ok }
end
end
def comment
@post = Post.find(params[:id])
@user = User.find(session[:user_id])
@text = (params[:comment].values_at “text”).to_s
@post.comment.create(:text => @text, :author => @user.screen_name, :author_id => @user.id)
flash[:notice] = “Komentarz został dodany”
redirect_to :action => ‘show’, :id => @post
end
end
Z góry dziękuję za wszelką pomoc, pozdrawiam!

Wychodzi na to że zanim zaczerpnąłem pomocy na forum byłem blisko rozwiązania problemu samemu, ale pobłądziłem
Dzięki Wam wszystkim że pomogliście mi wrócić na właściwy tor, pozdrawiam!