【发布时间】:2012-02-05 16:58:28
【问题描述】:
我有两个模型 ForumThread 和 Post 设置如下:
class ForumThread < ActiveRecord::Cached
has_many :posts
end
class Post < ActiveRecord::Cached
end
class CreateForumThreads < ActiveRecord::Migration
def self.up
create_table :forum_threads do |t|
t.column :thread_name, :text
end
add_index :forum_threads, :thread_name
end
def self.down
drop_table :forum_threads
end
end
class CreatePosts < ActiveRecord::Migration
def self.up
create_table :posts do |t|
t.column :post_body, :text
t.integer :forum_thread_id, :null => false
t.integer :priority
end
end
def self.down
drop_table :posts
end
end
我想创建一个查询,返回所有论坛线程,其中每个线程中至少有一个优先级为 1 的帖子。 如何创建此查询?
我一直在考虑像ForumThread.joins(:posts).select(:priority => 1) 这样的东西。我对 Active Record 比较陌生(而且对加入完全不熟悉),因此不胜感激。
【问题讨论】:
标签: ruby-on-rails activerecord join