【问题标题】:ruby onrails join tableruby on rails 连接表
【发布时间】:2011-05-10 03:06:20
【问题描述】:

我正在尝试以 Rails 方式加入我的桌子。

这是 sql 查询

SELECT * FROM `ads` left join `state` ON ads.state_id = states.id

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

    Rails guides是你的朋友=)

    【讨论】:

      【解决方案2】:

      http://api.rubyonrails.org/classes/ActiveRecord/Base.html

      Student.joins(:schools).where(:schools => { :type => 'public' })
      Student.joins(:schools).where('schools.type' => 'public' )
      

      Rails ActiveRecord :joins with LEFT JOIN instead of INNER JOIN:

      User.find(:all, :limit => 10,
              :joins => "LEFT JOIN `user_points` ON user_points.user_id = users.id" ,
              :select => "users.*, count(user_points.id)", :group =>
              "user_points.user_id")
      

      【讨论】:

        【解决方案3】:

        如果您使用的是 rails 5,那么您可以使用它 -

        Ad.left_outer_joins(:states)   
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-11-28
          • 2010-10-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多