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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:51:44+00:00 2026-06-18T07:51:44+00:00

I have a model with a has_one association through another model. class Publisher has_many

  • 0

I have a model with a has_one association through another model.

class Publisher
  has_many :books
end

class Book
  belongs_to :publisher
  has_one :author
end

class Author
  belongs_to :book
  has_one :publisher, :through => :book
end

In my Rails code, I can call author.publisher without a problem, so that all works nicely. However in my specs (using Rspec and FactoryGirl, the association just doesn’t seem to work. Here are my FactoryGirl definitions:

Factory.define :author do |a|
  a.association :book
end

Factory.define :book do |b|
  b.association :publisher
end

Factory.define :publisher
end

(Omitted most of the attributes from the factories).

Now in my specs, I can do the following

pub = Factory(:publisher)
book = Factory(:book, :publisher => pub)
author = Factory(:author, :book => book)

author.book # => Returns book
author.book.publisher # => Returns publisher 
author.publisher # => nil

So why doesn’t my through association work?

  • 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-18T07:51:45+00:00Added an answer on June 18, 2026 at 7:51 am

    In factory_girl/factory_girl_rails 4.1.0, the following works for me:

    factories.rb

    FactoryGirl.define do
    
      factory :author do
        book
      end
    
      factory :book do
        publisher
      end
    
      factory :publisher do
      end
    
    end
    

    In the rails console:

    pub = FactoryGirl.create(:publisher)
    #=> #<Publisher id: 1, created_at: "2013-01-30 13:35:26", updated_at: "2013-01-30 13:35:26">
    book = FactoryGirl.create(:book, :publisher => pub)
    #=> #<Book id: 1, publisher_id: 1, created_at: "2013-01-30 13:36:23", updated_at: "2013-01-30 13:36:23">
    author = FactoryGirl.create(:author, :book => book)
    #=> #<Author id: 1, book_id: 1, created_at: "2013-01-30 13:36:57", updated_at: "2013-01-30 13:36:57">
    author.book
    #=> #<Book id: 1, publisher_id: 1, created_at: "2013-01-30 13:36:23", updated_at: "2013-01-30 13:36:23">
    author.book.publisher
    #=> #<Publisher id: 1, created_at: "2013-01-30 13:35:26", updated_at: "2013-01-30 13:35:26">
    author.publisher
    #=> #<Publisher id: 1, created_at: "2013-01-30 13:35:26", updated_at: "2013-01-30 13:35:26">
    

    Curiously, the last author.publisher requires an additional SELECT operation, whereas author.book.publisher does not (perhaps related to the nil you observe):

    Publisher Load (0.2ms)  SELECT "publishers".* FROM "publishers"
      INNER JOIN "books" ON "publishers"."id" = "books"."publisher_id"
      WHERE "books"."id" = 1 LIMIT 1
    

    The same thing happens if you use Publisher.create, Book.create, Author.create instead of FactoryGirl.create, so this is not factory girl behaviour but rails behaviour, something to do with how through associations are cached.

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

Sidebar

Related Questions

I have 3 models: User has_many :questions has_many :corrections end Question has_one :correction belongs_to
I have the following model: class Advisor < ActiveRecord::Base belongs_to :course end class Course
I have a has_many through association setup between a song model and an artist
I have three models: class ReleaseItem < ActiveRecord::Base has_many :pack_release_items has_one :pack, :through =>
I have the following model associations: class Slider < ActiveRecord::Base has_one :featured_happening, :as =>
I have a simple has_one/belongs_to relationship between two models. This is a new association
I have a model called Contact which has_one guest like so. class Contact <
I have a Meeting model: class Meeting < ActiveRecord::Base has_one :location, :class_name => MeetingLocation,
I have a sentence and correction model class Sentence < ActiveRecord::Base has_one :correction class
I'm fairly new to rails and have 5 associations made through belongs_to on my

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.