Rails always include (join) on initialize
Posted
by
Seth
on Stack Overflow
See other posts from Stack Overflow
or by Seth
Published on 2010-12-28T02:51:57Z
Indexed on
2010/12/28
2:53 UTC
Read the original article
Hit count: 203
Hello, I have a User model as illustrated below:
class User < ActiveRecord
belongs_to :college
belongs_to :class_level
end
I want to ALWAYS join with those other two tables returning one simplified User object. How do I accomplish this in my User model. I'm aware that I can do this in another model:
class Foo < ActiveRecord
has_many :users, :include => [:college, :class_level]
end
But I want to do this in my User model, so Foo.users will either be eager loaded OR be joined already. Is there a way to create an initialize this in the User model?
© Stack Overflow or respective owner