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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T22:51:35+00:00 2026-05-28T22:51:35+00:00

I’m trying to test a model that has an implicit has_many association, and am

  • 0

I’m trying to test a model that has an implicit has_many association, and am having some difficulties.

I have a table A, with column B_ID, where B is basically a foreign key – except there is no table B in my database, or an active record class associated with object B. There is also table C that has column a B_ID.

In the model for table C we have:

# implicit has_many :alphas
def alphas
   Alpha.where(:b_id => b_id).order(:xyz)
end

This database structure makes sense for the data I have, and the non-test code works fine.

My test code almost works, and I hope I am just missing something simple.

I have factories defined for A and C, and I have a test:

a1 = Factory(:alpha, :b_id => 123, :xyz => 100)
a2 = Factory(:alpha, :b_id => 123, :xyz => 200)
c1 = Factory(:c, :b_id => 123)

puts c1.alphas.count
puts c1.alphas.first

c1.alphas.first.should == a1

The output is:

2
nil
<test fails>

Changing the number of A objects that share the B_ID result in the c1.alphas.count changing, but I can’t seem to actually inside the implicit association and get an A object back – instead I always get nil. There are other methods that in my C model that I can’t test because those methods need to access fields on individual A objects.

Does anybody have any insight into what is going behind the scenes here, or what I might do to get around this? Thanks.

  • 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-28T22:51:36+00:00Added an answer on May 28, 2026 at 10:51 pm

    Take a look at this for an example to have an admin_user with the role of Admin.

    https://github.com/drhenner/ror_ecommerce/blob/master/spec/factories/user.rb

    In this case roles are not a factory.

    I find it best to do this like the following though.

      @order = Factory(:order)
      order_item = Factory(:order_item, :total => 5.52 )
      @order.stubs(:order_items).returns([order_item, order_item])
    

    or

      @order = Factory(:order)
      order_item = Factory(:order_item, :total => 5.52, :order => @order )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I'm trying to create an if statement in PHP that prevents a single post
I am trying to loop through a bunch of documents I have to put

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.