How to get loaded conditional for lazy loading later with ActiveRecord and Rails

Let me show you the code first.

class User
  has_and_belongs_to_many :roles
  named_scope :employees, { :conditions => ["roles.name = 'Employee'"], :include => :roles }
end

      

OK, so later in the controller I wanted to search for all employees. I've set up a named scope to help do this with concatenation and conditional lookups. But the problem is I want to show all these user roles, but it will only show the employee role.

Is there anyway that I can say "user.roles" and be lazy loaded into the view after I've already loaded it?

+1


a source to share


1 answer


The documentation for habtm

(and also has_many

) states that you can access the collection using an optional parameter:

collection (force_reload = false)

Returns an array of all related objects. An empty array is if they were not found.



You can reload roles with user.roles(true)

.

+2


a source







All Articles