HABTM 查询帮助 (Rails 3)

发布于 2024-10-24 20:55:57 字数 926 浏览 2 评论 0原文

我有以下模型:

#user.rb
class User < ActiveRecord::Base
  has_and_belongs_to_many :groups
end

#group.rb
class Group < ActiveRecord::Base
  has_and_belongs_to_many :users
end

#join table migration
class CreateGroupUserJoinTable < ActiveRecord::Migration
  def self.up
    create_table :groups_users, :id => false do |t|
      t.integer :user_id
      t.integer :group_id
    end
  end

  def self.down
    drop_table :groups_users
  end
end

我需要进行以下查询:

@group = Group.find(:all, :include => users, :conditions => ["users count < ?", group_size]).first

但这给了我以下错误:

SQLite3::SQLException: near "count": syntax error: SELECT "groups".* FROM "groups" WHERE (users count < 2) LIMIT 1

我也尝试过这个:

@group = Group.where("users count < ?", group_size).first

但我得到了同样的错误。我做错了什么?

I have the following models:

#user.rb
class User < ActiveRecord::Base
  has_and_belongs_to_many :groups
end

#group.rb
class Group < ActiveRecord::Base
  has_and_belongs_to_many :users
end

#join table migration
class CreateGroupUserJoinTable < ActiveRecord::Migration
  def self.up
    create_table :groups_users, :id => false do |t|
      t.integer :user_id
      t.integer :group_id
    end
  end

  def self.down
    drop_table :groups_users
  end
end

I need to make the following query:

@group = Group.find(:all, :include => users, :conditions => ["users count < ?", group_size]).first

But this gives me the following error:

SQLite3::SQLException: near "count": syntax error: SELECT "groups".* FROM "groups" WHERE (users count < 2) LIMIT 1

I've also tried this:

@group = Group.where("users count < ?", group_size).first

But I get the same error. What am I doing wrong?

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

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

发布评论

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

评论(2

热鲨 2024-10-31 20:55:57
Group.select("groups.*, count(users.id) AS user_count").joins(:users).group("groups.id").having(["count(users.id) < ?", group_size])

我想这可能会为你做...

Group.select("groups.*, count(users.id) AS user_count").joins(:users).group("groups.id").having(["count(users.id) < ?", group_size])

I think that'll probably do it for you...

把梦留给海 2024-10-31 20:55:57

users.count 是您所需要的 - 请注意您留了一个空格。您收到语法错误,而不是一些奇怪的错误。

@group = Group.find(:all,
                    :include => users,
                    :conditions => ["users.count < ?", group_size]).first

现在,如果您确实只想要第一个条目,那么您应该这样做:

@group = Group.first(:include => users,
                     :conditions => ["users.count < ?", group_size])

注意我正在调用 #first,而不是 #find#find 将很快被弃用 - 请使用 #all#first 代替。

users.count is what you need - notice you left a space. You're getting a syntax error, not some strange error.

@group = Group.find(:all,
                    :include => users,
                    :conditions => ["users.count < ?", group_size]).first

Now, if you only really wanted the first entry, you should really do:

@group = Group.first(:include => users,
                     :conditions => ["users.count < ?", group_size])

Notice I'm calling #first, not #find. #find will be deprecated shortly - use #all, and #first instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文