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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:56:10+00:00 2026-06-14T10:56:10+00:00

I have built a nested resources between question and answer model. This is form

  • 0

I have built a nested resources between question and answer model. This is form i used to create question, i just wrap info relevant:

<fieldset>
  <legend>Question</legend>
    <%= render 'new_question_fields', question_form: question_form %>

  <legend>Answer</legend>
    <%= question_form.simple_fields_for :answers do |answer_form| %>
      <%= render 'answer', f: answer_form %>
    <% end %>
</fieldset>

This is my answer partial:

<div class="answer_fields well fields">
  <%= f.input :correct, label: 'This answer is correct.' %>
  <%= f.input :content, input_html: { rows: 3, class: 'span6' } %>
</div>

This is my index page, display questions:

<ul class="questions">
  <% @questions.each do |question| %>
    <li><%= question.content %></li>
    <ol class="answers">
      <% question.answers.each do |answer| %>
        <li><%= answer.content %></li>
     <% end %>  
    </ol>
  <% end %>
</ul>

On the page to create new question, I built 4 fields for 4 answer of question. This is html code of 4 textarea when they are render in html:

<textarea cols="40" id="question_answers_attributes_0_content" name="question[answers_attributes][0][content]" rows="3"></textarea>
<textarea cols="40" id="question_answers_attributes_1_content" name="question[answers_attributes][1][content]" rows="3"></textarea>
<textarea cols="40" id="question_answers_attributes_2_content" name="question[answers_attributes][2][content]" rows="3"></textarea>
<textarea cols="40" id="question_answers_attributes_3_content" name="question[answers_attributes][3][content]" rows="3"></textarea>

Say, i have order of fields is 0,1,2,3, but when i save questions, the orders of answer is reverse, example:

If i type 4 answers A,B,C,D corresponding the order of textarea on form is 0,1,2,3, when the question is saved, it display this: D,C,B,A, it means it saved value of textarea question[answers_attributes][3][content] first, then question[answers_attributes][2][content]…

Update: This is my index and create action in Question controller:

def index
  @questions = Question.where("user_id = ?", current_user.id).paginate(page: params[:page], per_page: 10)
end

def create
  @question = Question.new(params[:question])
  @question.question_type_id = params[:question_type_id]
  @question.user_id = current_user.id

  if @question.save
    flash[:success] = "Successfully created question."
    redirect_to questions_url
  else
    render 'new'
  end
end

My answer model:

class Answer < ActiveRecord::Base
  attr_accessible :content, :question_id, :correct
  belongs_to :question
end

what is happen when question is save? is this because method save of rails or my display form?

  • 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-14T10:56:11+00:00Added an answer on June 14, 2026 at 10:56 am
    • question.answers.order("created_at ASC")... or;
    • question.answers.order("created_at DESC")...

    One of it should reverse order.

    —

    But more on that topic – I guess it’s about how stack is handled there. You are creating each answer by passing it as an attribute for question during its’ creation and that’s probably where’s the mix. Logs from Rails & database w/ log_level :debug should help to see how it goes into the database.

    Also id ASC probably works for you because your ids increment with time anyway and then it’s just the same as created_at ASC – but relying on ids may not always be a good approach since sometimes ids may be random.

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

Sidebar

Related Questions

I have a nested model structure that looks like this: resources :users, :path =>
I have nested resources like this in my routes.rb - ( my rake:routes gist
I have a nested route: resources :stories do resources :comments end this is my
I have built a game in HTML5 and a web form posts data to
Is it possible for a model to belong_to, two models and have a nested
I have a complex form for a model Schedule that belongs to a parent
I have the following nested resources in my routes.rb file. The inner resource specifies
I'm building a one page form that will create a model and it's relations.
I have this deeply nested list (list of lists) and I want to replace
I have a nested polymorphic resource, question, which may be nested beneath an exam.

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.