I am trying to create a query that finds all the Posts that belong to the same Topic id. I believe I’m on the right track, but all @posts returns is every post in the database.
Topics controller:
def show
@topic = Topic.find(params[:id])
@title = @topic.name
@posts = Post.where('topic' == @topic).order("updated_at").page(params[:page]).per(10) #not working. still just fetches all posts
respond_with(@posts)
end
Topic model:
class Topic < ActiveRecord::Base
has_many :posts, :dependent => :destroy
attr_accessible :name
end
Post model:
class Post < ActiveRecord::Base
belongs_to :topic, :touch => true
accepts_nested_attributes_for :topic
attr_accessible :name, :title, :content, :topic, :topic_attributes
end
I would recommend you to use the association in the model to get all the posts. you could do it like this: