【问题标题】:Scope to fetch Associated data from Database Rails 3从 Database Rails 3 中获取关联数据的范围
【发布时间】:2013-05-22 08:34:33
【问题描述】:

基本上我想使用范围通过关联来获取数据。

用户和帖子之间的关联。

 belongs_to :user # on posts model
 has_many :posts, :dependent => :destroy #on user model

查询

数据库有不同用户创建的帖子的数量记录,所以我想获取所有那些 post.user.name 中没有模式(整数等)的帖子。

我想使用

Post.scope_name

【问题讨论】:

  • 你能举一个例子说明“post.user.name 中没有模式(整数等)”
  • 阿米特,不知何故,我已经使用我在答案中提到的范围找出了所需的结果。

标签: ruby-on-rails-3 scope associations


【解决方案1】:

我做了这样的事情

scope :posts_by, joins("LEFT OUTER JOIN users u ON u.id = posts.user_id").where('users.name REGEXP ? ', "my_pattern")

【讨论】:

    猜你喜欢
    • 2011-08-14
    • 2012-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-09
    • 1970-01-01
    相关资源
    最近更新 更多