Rails 表关联查询.大神进

new_birld · May 28, 2014 · Last by messiahxu replied at May 28, 2014 · 1840 hits

ip 有 creator_id 和 update_user_id ip belongs_to user 我要对 ip 的创建者排序,还有更新者排序 怎么写呢? `class Ip < ActiveRecord::Base belongs_to :user , :foreign_key => [:creator_id ,:update_user_id] end

class user < ActiveRecord::Base has_many :ips, :foreign_key => [:creator_id ,:update_user_id] end` 只有更新过的 ip 有更新者,没有跟新过的 ip update_user_id 为空

对 ip 的创建者排序 ip Ip.includes(:user).order("users.user_name asc") 对 ip 的更新者怎么排序 ip

错误:Ip.joins(:user).order("users.user_name asc")

#1 楼 @hz_qiuyuanxin 我那样也是可以的

#1 楼 @hz_qiuyuanxin 主要是想知道怎么对 ip 的更新者排序

我印象中没有给 foreign_key 设数组的用法啊。

Ip.joins("inner join users on users.id = ips.update_user_id").order("users.user_name asc")

这样?

You need to Sign in before reply, if you don't have an account, please Sign up first.