Many_to_many、sti 和 will 分页

发布于 2024-08-10 13:15:49 字数 821 浏览 9 评论 0原文

我有以下代码

class Category < ActiveRecord::Base
  has_many :categorizations
  has_many :posts, :through => :categorizations
end

class Post < ActiveRecord::Base
  has_many :categorizations
  has_many :categories, :through => :categorizations
end

class Categorization < ActiveRecord::Base
  belongs_to :post
  belongs_to :category
end

class NewsArticle < Post
end

,看起来不错。我试图从类别中获取所有 NewsArticle

@news_articles = NewsArticle.paginate_by_category_id params[:category],
  :page => params[:page], :per_page => 10,
  :order => 'posts.created_at DESC'

,我知道

 NoMethodError in News articlesController#category

undefined method `find_all_by_category' for #<Class:0x103cb05d0>

我能做些什么来解决这个问题?

i have following code

class Category < ActiveRecord::Base
  has_many :categorizations
  has_many :posts, :through => :categorizations
end

class Post < ActiveRecord::Base
  has_many :categorizations
  has_many :categories, :through => :categorizations
end

class Categorization < ActiveRecord::Base
  belongs_to :post
  belongs_to :category
end

class NewsArticle < Post
end

ok, looks good. I'me trying to get all NewsArticle's from category

@news_articles = NewsArticle.paginate_by_category_id params[:category],
  :page => params[:page], :per_page => 10,
  :order => 'posts.created_at DESC'

and i see

 NoMethodError in News articlesController#category

undefined method `find_all_by_category' for #<Class:0x103cb05d0>

what can i do to solve this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

囍笑 2024-08-17 13:15:49

怎么样:

@news_articles = NewsArticle.paginate(:conditions => { :category_id => params[:category_id].to_i }, :page => params[:page], :per_page => 10, :order => 'posts.created_at DESC')

您是否将类别 ID 作为 params[:category]params[:category_id] 传递?如果您不确定可以在视图中debug(params)

How about:

@news_articles = NewsArticle.paginate(:conditions => { :category_id => params[:category_id].to_i }, :page => params[:page], :per_page => 10, :order => 'posts.created_at DESC')

Are you passing the category id as params[:category] or params[:category_id]? If you're not sure you can debug(params) in your view.

下雨或天晴 2024-08-17 13:15:49

我将添加命名范围并将其与分页一起使用:

class Post < ActiveRecord::Base
  ..
  named_scope :newest, :order => 'posts.created_at DESC'
  named_scope :by_category, lambda { |category_id| { 
    :joins => :categories,
    :conditions => ['categories.category_id = ?', category_id]
  } }
end

@news_articles = NewsArticle.newest.by_category(params[:category]).paginate(
  :page => params[:page], :per_page => 10
  )

I would add named scopes and use them with paginate:

class Post < ActiveRecord::Base
  ..
  named_scope :newest, :order => 'posts.created_at DESC'
  named_scope :by_category, lambda { |category_id| { 
    :joins => :categories,
    :conditions => ['categories.category_id = ?', category_id]
  } }
end

@news_articles = NewsArticle.newest.by_category(params[:category]).paginate(
  :page => params[:page], :per_page => 10
  )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文