我想創建一個查詢,查找屬於同一主題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