我有以下SQL語句: SELECT article_id FROM publications WHERE category_id IN (SELECT id FROM categories WHERE parent_id = 3)
ActiveRecord的: 「凡在」 SQL語句
如何將它轉換到Rails的ActiveRecord?我試過:Article.find(:all, :conditions => ["publications.category_id IN(?)", 3], :include => [:publications])
,它返回空數組。
型號:
class Article < ActiveRecord::Base
has_many :publications
has_many :categories, :through => :publications
class Category < ActiveRecord::Base
belongs_to :parent, :class_name => 'Category'
has_many :children, :class_name => 'Category', :foreign_key => :parent_id
has_many :articles, :through => :publications
has_many :publications
class Publication < ActiveRecord::Base
belongs_to :article
belongs_to :category
您可以發佈您的關聯碼(的has_many,belongs_to的,等等)出版,文章和類別?我懷疑你可能在那裏出了問題。 – Yardboy