0

Having set up a has_many through relationship, I'm trying to iterate through associated B objects in the view of an object A. I.e. something like

<% for q in @survey.questions do %>
  <%= q.name %> <br/>
<% end %>

yields nothing, while

<%= @survey.questions %>

yields

#<ActiveRecord::Associations::CollectionProxy::ActiveRecord_Associations_CollectionProxy_Question:0x007f9859f221e8>

How could (should) I access these?


Here's the Controller

class SurveysController < ApplicationController
  before_action :set_survey, only: [:show, :edit, :update, :destroy]


  def index
    @surveys = Survey.all
  end

  def show
  end

  def new
    @survey = Survey.new
  end

  def edit
  end

  def create
    @survey = Survey.new(survey_params)

    respond_to do |format|
      if @survey.save
        format.html { redirect_to @survey, notice: 'Survey was successfully created.' }
        format.json { render action: 'show', status: :created, location: @survey }
      else
        format.html { render action: 'new' }
        format.json { render json: @survey.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
    respond_to do |format|
      if @survey.update(survey_params)
        format.html { redirect_to @survey, notice: 'Survey was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @survey.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    @survey.destroy
    respond_to do |format|
      format.html { redirect_to surveys_url }
      format.json { head :no_content }
    end
  end

  private
    def set_survey
      @survey = Survey.find(params[:id])
    end
end

And here's the Models

class Survey < ActiveRecord::Base
  has_many :assignments
  has_many :questions, through: :assignments
end

.

class Question < ActiveRecord::Base
  has_many :assignments
  has_many :surveys, through: :assignments
end

.

class Assignment < ActiveRecord::Base
  belongs_to :survey
  belongs_to :question
end
jiku
  • 283
  • 5
  • 16
  • Can you please add your model and controller code to the question? – Powers Dec 08 '13 at 15:04
  • how is @survey defined? – Philip7899 Dec 08 '13 at 15:50
  • Fixed this with a combination of http://stackoverflow.com/questions/17982904/unpermitted-parameters-in-rails-4 and http://stackoverflow.com/questions/17436264/how-to-use-rails-4-strong-parameters-with-has-many-through-association – jiku Dec 09 '13 at 05:09

3 Answers3

0

Try this:

<% @survey.questions.each do |q| %>
  <%= q.name %> <br/>
<% end %>

The for loop should be avoided in Ruby. From this question, it looks like for loops operate on arrays, so something like this might also work (again, this is not the recommended solution):

<% for q in @survey.questions.to_a do %>
  <%= q.name %> <br/>
<% end %>

Update

I think you need to make your associations singular in the Assignment model:

class Assignment < ActiveRecord::Base
  belongs_to :survey
  belongs_to :question
end

I created a quiz on many to many relationships that you might find helpful.

Community
  • 1
  • 1
Powers
  • 18,150
  • 10
  • 103
  • 108
  • Sorry! That'll teach me not to copypaste everything. They actually were singular. Updating the post to reflect it. – jiku Dec 08 '13 at 16:22
  • Looking at your guide (thanks!) could it be something to do with attr_accessible? I.e. do I have to make the :name in the Question class accessible? – jiku Dec 08 '13 at 16:25
  • Btw, I went through your guide and it led to this question http://stackoverflow.com/questions/20461916/problems-getting-has-many-x-through-y-to-work-in-the-console – jiku Dec 09 '13 at 02:12
0

As far as I can tell Powers answer should work. Try checking if the @survey actually has any questions. Add this in the page somewhere <%= @survey.questions.count %>

gduq
  • 1,434
  • 11
  • 13
0

Are you sure the @survey in question really has assignments associated to it? Can you see them on the Rails console?

janfoeh
  • 10,243
  • 2
  • 31
  • 56