开发者

Rails 3 undefined method `model_name' for Array:Class

开发者 https://www.devze.com 2023-03-04 16:33 出处:网络
answers_controller: @questions = Question.order(\"qtype, position\") @ans = Answer.where(user_id = current_user.id)

answers_controller:

@questions = Question.order("qtype, position")
@ans = Answer.where(user_id = current_user.id)
@answers = @questions.map { |q| [q, @ans.find_by_question_id(q.id)] }

view:

<%= form_for @answers, :remote => true do |form| %>

Is the error because @answers is is mapped? Any help is appreciated.

UPDATE:

Answer model:

belongs_to :user
belongs_to :question

User model:

has_many :answers

Question model:

has_many :answers

UPD开发者_JAVA技巧ATE 2: I copied and pasted the following into the controller.

@answers = current_user.answers.questions

I got the following:

undefined method `questions' for #<Class:0x1032d6d98>

I tried going adopting the second approach - User model:

has_many :answers, :through => :answers

controller:

@questions = current_user.questions

and got:

undefined method `questions' for #<User:0x103569ec0>
app/controllers/answers_controller.rb:18:in `index'


Yes, this is the reason. You probably wanted to do: current_user.answers.questions

Which, if you have your associations set as needed, will return exactly what you want (by using joins behind the scenes)

update:

Now, by looking at your associations, I'm not sure, what you're trying to accomplish. Won't @answers = current_user.answers return all answers, and then in the view you can use answer and corresponding question (and still use form_for @answers). However if you want to have @questions instead, you can do something like this:

First, associate user with questions


class User
  has_many :questions, :through => :answers
end

The in your controller:


@questions = current_user.questions

Hope that it helps.


@answers = @questions.map { |q| [q, @ans.find_by_question_id(q.id)] }

This gives you an array of arrays which in turn contain the question and another array of all of the question's answers. Like this:

[[q1, [q1_answer1, q1_answer2]], [q2, [q2_answer1, q2_answer2,...]], ...]

So each of the answers are in an array that itself is 2 levels deep inside @answers.

As for your problem when you reference current_user.answers, you get back an array of answers.

Associations are for an instance of a class, not a collection of them. That is, you can call answer.question to get the question associated with a given answer. However, you can't just call answers.questions as above, because a)answers is an array, not an Answer instance, and b) the association is 'question', not 'questions'

If you're trying to get all questions for all answers, you need to some sort of operation on each answer, like this:

questions = current_user.answers.map{|answer| answer.question}

which will map each question for each answer into an array. Note that if a user has multiple answers for a question, that question will end up in the array multiple times. It can be deduped with the 'uniq' method.


If I understand correctly, you want to list all answers, but in the same order as your questions.

I am assuming your models look like

class User
  has_many :questions, :through => :answers
  has_many :answers
end

class Question
  has_many :answers
  has_many :users, :through => :answers
end

class Answer
  belongs_to :user
  belongs_to :question 
end

Getting the questions of the current_user should then just become:

@questions = current_user.questions.order("qtype, position")

getting the corresponding answers then becomes

@answers = @questions.map{|q| q.answer}

Hope this helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消