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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:11:10+00:00 2026-06-14T09:11:10+00:00

I have two model A and B. A has_many B’s. B has an attribute

  • 0

I have two model A and B. A has_many B’s. B has an attribute :number

What is the rails way (I could do some coding with each, but that’s not the point) to find if an A has a B object with a given number ?

I’ve tried find but since it’s an association, it gives me this error:

>> bs.find{|f| f.number == 8}
>> ActiveRecord::RecordNotFound: Couldn't find A without an ID

EDIT

To make more clear.

If I had to code this would be something like:

def is_number_in_use(number)?
    self.bs.each do |b| #Consider bs as the has_many association between A and B
        return true if b.numero == number
    end
    return false
end
  • 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-06-14T09:11:11+00:00Added an answer on June 14, 2026 at 9:11 am

    Is that better?

    a.bs.select{ |b| b.number == 8 }.any? #=> return true if a has one b o more with number == 8
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two models/tables in my Rails application: discussions and comments. Each discussion has_many
hi I have two rails model class Feedback < ActiveRecord::Base has_many :conversations, :dependent =>
so i have got two models with each one controller: Model Project has_many Themes
I currently have two different models: User and Project . The User model has
In my Core Data model I have two entities: List and Patient. List has
I'm writing a Rails app in which I have two models: a Machine model
Hypothetical scenario: I have two models: Author and Book . The Book model has
I have three models: System_Contact System Contact_list The Contact_List model has two fields: contact
I have two models, a Charity model and a Milestone model. A charity has_many
I have two models, model_1 and model_2. model_1.rb: has_many :model_2s model_2.rb: belongs_to :model_1 But

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.