Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7088585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:49:08+00:00 2026-05-28T07:49:08+00:00

I have a strange problem. I have two models Users and roles with a

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T07:49:08+00:00Added an answer on May 28, 2026 at 7:49 am

    You mentioned that you are doing this in a validation before the model is saved. This is why it’s failing. Since the User is not yet saved, it doesn’t have an id in your database, so there’s no way for it to search your associations.

    The reason the first line works (self.roles.to_s) is that Rails memorizes any roles you add to your user, but does not save them to the database until you save the user. Once you save the user, your second/third lines would work fine:

    user = User.new
    user.roles << Role.first
    user.roles
    # => [#<Role id: 1, name: "Administrator" ...>]
    user.roles.find_by_name("Administrator")
    # => nil
    user.save  # This inserts both the new user AND the associations
    user.roles.find_by_name("Administrator")
    # => #<Role id: 1, name: "Administrator" ...>
    

    If you have to work with this in your validations, you might try using Enumerable’s find method to search the roles array instead:

    user = User.new
    user.roles << Role.first
    user.roles
    # => [#<Role id: 1, name: "Administrator" ...>]
    user.roles.find { |role| role.name == "Administrator" }
    # => #<Role id: 1, name: "Administrator" ...>
    user.roles.find { |role| role.name == "Foo" }
    # => nil
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a strange problem between two div's , there is a space below
I have a straightforward relation between two models: Asset and Update. both models (minus
I have a strange problem ... I have two projects in Xcode: PR1 and
I have one strange problem.I have two UItextfields in my view and a button.When
Good Morning, I have Stuck in a strange problem. I want to open PDF
I have a strange problem... I have a JFrame and added two JInternalFrame I
This is a strange problem. I have two html pop-up windows, just hidden divs
I have strange problem with sharepoint and ajax functionality. We have an UpdatePanel placed
i have strange problem doing reporting: i have numerous clients with different issued invoices.
We have very strange problem, one of our applications is continually querying server by

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.