Advertisement
Guest User

Untitled

a guest
Mar 30th, 2015
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.46 KB | None | 0 0
  1. class Foo < ActiveRecord::Base
  2. has_one :bar, ->(current_user) { where(user_id:current_user) }
  3. end
  4.  
  5. class Bar < ActiveRecord::Base
  6. belongs_to :foo
  7. belongs_to :user
  8. end
  9.  
  10.  
  11. class User < ActiveRecord::Base
  12. has_one :bar
  13. end
  14.  
  15. > Foo.first.bar(2)
  16. SQL => Foo Load (1.2ms) SELECT "foos".* FROM "foos" ORDER BY "foos"."id" ASC LIMIT 1
  17. Bar Load (0.7ms) SELECT "bars".* FROM "bars" WHERE "bars"."foo_id" = ?
  18. AND "bars"."user_id" = 1 LIMIT 1 [["foo_id", 1]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement