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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:06:46+00:00 2026-06-15T21:06:46+00:00

I have two ActiveRecord models, A and B . A has_many :B and B

  • 0

I have two ActiveRecord models, A and B. A has_many :B and B belongs_to :A. Naturally, B has an a_id column.

I have a bunch of A‘s and every time I create a new B, I want to associate it with an A if certain conditions hold.

Currently, I’m retrieving the possible A‘s and linking one to a B like so:

class B < ActiveRecord::Base

    attr_accessible :a_id
    belongs_to :a

    def link_to_a
        possible_as = A.where(some: conditions)
        self.a = possible_as.find_by_other-foreign-key_id(self.other_id) if possible_as != nil
        # Then I have to perform an operation on the b's a such as:
        self.a.linked_to_b_at = Time.now if self.a != nil
    end
end

This seems smelly. Is there a better way to link the two models? I thought making the has_many and belongs_to relationships explicit would help me. I must be missing something.

  • 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-15T21:06:48+00:00Added an answer on June 15, 2026 at 9:06 pm

    If B has a belongs_to relationship with A, then the way you created your B records is incorrect. You got to use the build method to create dependent records.

    For example:

    def create_b_records
     a = A.find(some_id)
     a.build_b(new_record_attributes)
     a.save 
    end
    

    Now, with that, retrieving all B records for a particular set of A records becomes quite straightforward:

    possible_as = A.where(some_condition)
    possible_as.each do |possible_a|
     possible_a.b #Do whatever you want to do with these B records
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two models, 'product' with 'belongs_to' and 'category' with 'has_many'. Product has a
I have two models, Landscape: class Landscape < ActiveRecord::Base has_many :images, :as => :imageable
I have two models. order and line_item. class Order < ActiveRecord::Base has_many :line_items has_many
I have two models: Program < ActiveRecord::Base has_many :events def federal_financing events.sum(&:federal_financing) end def
Let's say I have two models like so: class Comment < ActiveRecord::Base belongs_to :user
Let's say I have two models like so: class Comment < ActiveRecord::Base belongs_to :user
I have two Models here associated as such: class Order < ActiveRecord::Base belongs_to :customer
I have two models, Like and Photo. class Like < ActiveRecord::Base belongs_to :photo, :class_name
I have two models: class Wine belongs_to :region end class Region has_many :wines end
I have two models: class Group < ActiveRecord::Base belongs_to :sites end class Site <

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.