【问题标题】:Count records in a table NxN without model计算没有模型的表 NxN 中的记录
【发布时间】:2014-05-23 13:14:06
【问题描述】:

所以,我很怀疑。

在我的应用程序中,我有一个名为 user_profiles_mobile_models 的表,该表有两列: user_profile_id int(11) PK mobile_model_id int(11) PK

但我有两个模型,UserProfile 和 MobileModel。

class UserProfile
  ...
  has_and_belongs_to_many :mobile_models, join_table: 'user_profiles_mobile_models', order: 'name'
  ...

我需要计算有多少用户拥有 id 为 1、7、8 的移动模型...

有可能吗?

感谢和抱歉我的英语不好

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.1


    【解决方案1】:

    如果您的模型 MobileModel 中有一个

    has_and_belongs_to_many :user_profiles, join_table: 'user_profiles_mobile_models'
    

    那么,我们就可以使用了。

    MobileModel.where('id in (?)', [1,7,8]).map(&:user_profiles).count
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-07
      • 2023-03-20
      相关资源
      最近更新 更多