2016-07-22 62 views
0

HAS_ONE记录用下面的模型ActiveRecord的包括或加入在哪里

user.rb

# Columns 
# job_title :string 
# (..) 

has_one :spec 

spec.rb

# Columns 
# org_unit :string 
# department :string 
# room_number :string 

belongs_to :user 

...我能写:

irb(main):005:0> user.spec.department 
    Spec Load (0.0ms) SELECT "specs".* FROM "specs" WHERE "specs"."user_id" = ? 
    LIMIT ? [["user_id", 1], ["LIMIT", 1]] 
=> front-end 

但现在我想做在这两个类似:

User.where(job_title: "developer").where(self.spec.department: "front-end") 

有没有办法做到这一点?

回答

3

尝试:

User.where(job_title: "developer").joins(:spec).where(specs: {department: "front-end"}) 
+0

权。编辑我的答案。 – lunr

+0

可读且准确工作的解决方案。非常感谢你! –