ruby on rails join table

时间:2010-11-15 09:05:32

标签: ruby-on-rails

我正试图以rails方式加入我的桌子。

这是sql查询

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

3 个答案:

答案 0 :(得分:0)

Rails guides是你的朋友=)

答案 1 :(得分:0)

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")

答案 2 :(得分:0)

如果您使用滑轨5,则可以使用此-

Ad.left_outer_joins(:states)