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 574439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:49:51+00:00 2026-05-13T13:49:51+00:00

I am new to rails and ruby. I have a has_many association between user

  • 0

I am new to rails and ruby.

I have a has_many association between user and store

below is what I do:

@user = User.find_by_userid_and_password("someuser", "12345")
=> #<User id: 1, userid: "someuser", password: "12345", 
created_at: "2010-01-25 00:00:00", updated_at: "2010-01-25 00:00:00"> 
@user.stores
=> [#<Store id: 3, store_id: 3, store_name: "New Store 2", 
created_at: "2010-01-25 00:00:00", updated_at: "2010-01-25 00:00:00">, 
 #<Store id: 5, store_id: 5, store_name: "Store 14th and M", 
 created_at: "2010-01-25 00:00:00", updated_at: "2010-01-25 00:00:00">]

so basically i am first authenticating the user and then getting all the stores that a user belongs to. For that I get a list back. In that list of hashes i want to find out if anything is there with store_id == 4.

sequentially I do:

@user.stores.first.store_id==4
false
@user.stores.second.store_id==4
false

how can i make this in a loop? and is there any better way to do this.

  • 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-13T13:49:51+00:00Added an answer on May 13, 2026 at 1:49 pm

    Welcome to Rails,

    Your best approach here is probably not to use a loop, but to chain a find to your first finder.

    For example:

    @user.stores.find(store_id)

    This will leverage the db and be much faster.

    Check out the API

    If you do want to loop you do something like the following

    @user.stores.each do |store|
      @my_store = store if store.id == store_id
    end
    

    or

    @my_store = @user.stores.select{|s| s.id == store_id}

    or

    @contains_store = @user.stores.include?{|s| s.id == store_id}

    Good luck,

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to ruby on rails. So i think i have association problems. Given
I am new to Ruby on Rails, I have completed the Blog Tutorial .
I am fairly new to Ruby on Rails, and I clearly have an active
I am new to ruby on rails and I have just started watching rails
New to both Ruby and Rails but I'm book educated by now (which apparently
First of all, let me say I am very new to rails, have been
I have a Ruby on Rails application that has two active environments, Stage and
I'm still new to Ruby on Rails and I'm currently having a issue with
I have built a ruby on rails app that lets users track their workouts.
I have built a ruby on rails app that lets users track their workouts.

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.