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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:34:36+00:00 2026-05-31T13:34:36+00:00

I am developing a test application to learn Rails. While playing with associations I

  • 0

I am developing a test application to learn Rails. While playing with associations I am getting ActiveModel::MissingAttributeError: can't write unknown attribute 'linked_section_id' error.

Let me first explain requirement. An User selects a Section of a Book for viewing. At the end of each section there will be few questions which is linked to some another sections. If user clicks on any question link, he will be redirected to associated section.

So the entities relation is as described below:

  • A section can have multiple questions (one to many)
  • A question belongs to a single section only (many to one – flip side of one to many)
  • A question can be linked to a different section ( one to one)

Environment:
Ruby 1.9.3,
Rails 3.2.2
with all required gems

here are model classes:

class Section < ActiveRecord::Base

  # Associations
  has_many :questions

end

class Question < ActiveRecord::Base
  belongs_to  :section, :foreign_key => :section_id
  has_one :linked_section, :class_name => "Section", :foreign_key => "linked_section_id"
end

db/schema.rb
.
.
create_table "sections", :force => true do |t|
    t.string   "name"
    t.string   "content"
    t.datetime "created_at", :null => false
    t.datetime "updated_at", :null => false
  end
create_table "questions", :force => true do |t|
    t.string   "description"
    t.integer  "section_id"
    t.integer  "linked_section_id"
    t.datetime "created_at",        :null => false
    t.datetime "updated_at",        :null => false
  end
.
.

Here are the Specs.

question_spec.rb
require 'spec_helper'

describe Question do
  before do
      @question = Question.new(description:'Test')
    end

        subject { @question }

        # Check for attribute accessor methods
        it { should respond_to(:description) }
        it { should respond_to(:section) }
        it { should respond_to(:linked_section) }
        it { should respond_to(:linked_section_id) }
end

section_spec.rb
require 'spec_helper'

describe Section do
    before do
      @section = Section.new(name:'Test')
    end

        subject { @section }

        # Check for attribute accessor methods
        it { should respond_to(:name) }
        it { should respond_to(:questions) }

        # Sanity check, verifying that the @section object is initially valid
        it { should be_valid }

    describe "Question should have one action item while persisted" do
            pending "This is broken after changing entity relationship"
      before {
        @section = Section.new(name: 'Section 2')

        linked_section = Section.new(name: 'Action Section')
        linked_section.save

        question = Question.new(description: 'What next?')
        question.linked_section = linked_section
        @section.questions << question
        @section.save
        .
                .
        .

      }
      .
            .
            .
    end

end

Now the question_spec.rb passes without any error but getting ActiveModel::MissingAttributeError: can't write unknown attribute 'linked_section_id' error on line question.linked_section = linked_section.

but if you notice, following examples of question_spec.rb

it { should respond_to(:linked_section) }
it { should respond_to(:linked_section_id) }

passes without any error. It means Question has ‘linked_section_id’. So why rspec panics?

I have doubt on the relation defined for linking a question to different section. Can anyone please guide me to design models properly?

Thanks, Amit Patel

  • 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-31T13:34:37+00:00Added an answer on May 31, 2026 at 1:34 pm

    Your specs are checking whether or not the question responds to :linked_section_id (a reader method), not whether or not it responds to :linked_section_id= (a writer method). All fields in a table will have reader methods associated with them, and since you added a linked_section_id field in the table, the reader method is there.

    Whenever you add foreign keys to a table, it should be the *belongs_to* side of the association. Here, you used has_one. Switch that to belongs_to, and in the Section model, put

    has_one :linked_section, :class_name => 'Question'
    

    Hopefully this helps,

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

Sidebar

Related Questions

Can any one suggest an application to test my webservices. Basically I am developing
I am currently developing a Rails application, I'm trying to units test it. I
I'm just playing with Appcelerator's Titanium platform for developing mobile apps. My test application
I am developing a Rails v2.3 application. When I run the rspec test by
I am developing a PHP application using our XAMPP setup as a test server.
While developing products, we often need to create proprietary tools to test some of
While developing a Grails 1.0.5 app I'm appalled at how slow the grails test-app
Just wondering whats the best way to test Python CGI while developing a site?
I am developing application using Entity Framework. When i'm in debugging mode my test
I am developing a test application and running the whole thing on my work

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.