I have a strange problem.
I have two models Users and roles with a many to many relationships between them. I want to filter the roles with name 'Administrator' within the role collection of a user.
In the model this code
puts self.roles.to_s
Prints to screen: [Role id: 1, name: "Administrator", created_at: "2012-01-22 21:55:45", updated_at: "2012-01-22 21:55:45"]
But this code
puts self.roles.find_by_name('Administrator').to_s
doesn't print anything. And this one:
puts self.roles.find_by_name('Administrator').nil?
prints true!
Why isn't my find_by_name method not working? I tryied it in the console and it works well.
My code snippet is the following:
puts self.roles.to_s
puts self.roles.find_by_name('Administrator').to_s
puts self.roles.find_by_name('Administrator').nil?
And the output is the following:
[Role id: 1, name: "Administrator", created_at: "2012-01-22 21:55:45", updated_at: "2012-01-22 21:55:45"]
<none>
true
What am I doing wrong?? It has to be something stupid. This code is located in a validate method, so it is executed before the user is saved.