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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T03:15:51+00:00 2026-06-17T03:15:51+00:00

I have 3 tables: project has_many :answers questions –> questions every project must answer

  • 0

I have 3 tables:

project

has_many :answers

questions –> questions every project must answer

 has_many :answers

answers

belongs_to :project

belongs_to :question

So, when I create a new project, I would like to loop through the questions and create an answer record. Then the user can see all of the questions and enter their answers.

In the project controller, I’m trying to create those records.
But, the following isn’t working:

  before_create :create_answers

protected
def create_answers
  Questions.each do |i|
    self.answers.build  contact_id: self.contact_id, question_id: Question[i].id
  end
end

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-06-17T03:15:52+00:00Added an answer on June 17, 2026 at 3:15 am

    Instead of:

    Questions.each
    

    Do:

    Question.all.each
    

    All that code will need to go in the Project model, not the ProjectsController.

    And instead of:

    Question[i].id
    

    Do:

    i.id
    

    Make sure those attributes are attr_accessible as well.

    All together:

    before_create :build_answers
    
    protected
    def build_answers
      Question.all.each do |question|
        answers.build contact_id: contact_id, question_id: question.id
      end
    end
    

    And the attr_accessible bit:

    attr_accessible :contact_id, :question_id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables: entitytype and project . Here are the create table statements:
I have these tables and relationships: user has_many projects project has_many tasks task has_many
I have two tables, employees and project , in listbox2 , I show all
I have 2 tables AP and INV where both have the columns [PROJECT] and
I have been working on a project recently where a Player can create a
I have 2 tables. One has order elements (OE) and one has project information(PO).
I have a Doctrine2 project with three tables: Product , Category and Rating .
A project has 3 tables : client, invoices, deliveries The relations are: client has_many
I have 3 tables: Projects, Tasks, Timeentries Projects has_many tasks and has_many timeentries, :through
Consider the following situation. I have 3 tables in a resource planning project(created using

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.