我想创建一个查询,查找属于同一主题ID的所有帖子。我相信我在正确的轨道上,但所有@posts
返回是数据库中的每个帖子。Rails 3查询:查找所有同一主题的帖子
主题控制器:
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
主题模型:
class Topic < ActiveRecord::Base
has_many :posts, :dependent => :destroy
attr_accessible :name
end
Post模型:
class Post < ActiveRecord::Base
belongs_to :topic, :touch => true
accepts_nested_attributes_for :topic
attr_accessible :name, :title, :content, :topic, :topic_attributes
end
我想你的意思=>而不是= – DanneManne 2011-05-03 03:15:01
@DanneManne感谢您发现它。纠正! – thekindofme 2011-05-03 03:28:39