Reputation: 61
I have this request to database
@show = Question.where(:question_status => params[:id])
Then in @show variable I have this: [#<Question id: 38, user_id: 1, question: "hi", question_status: 1, created_at: "2013-06-04 18:32:28", updated_at: "2013-06-04 18:32:28">, #<Question id: 40, user_id: 1, question: "urll", question_status: 1, created_at: "2013-06-04 18:34:57", updated_at: "2013-06-04 18:34:57">, #<Question id: 41, user_id: 1, question: "urll", question_status: 1, created_at: "2013-06-04 18:35:31", updated_at: "2013-06-04 18:35:31">]
How get , for example, question field ?
I trying @show.question
but have error no defined method question
.
Upvotes: 0
Views: 79
Reputation: 2653
@show = Question.find_by_question_status(params[:id])
and @show.question
If you us where statement then use:
@show.each do |show|
show.question
end
Upvotes: 1
Reputation: 826
Use @show.map(&:question)
. This will give you all the questions in array.
@show.question gives you error no defined method question because you are trying to operate question on array @show.
If you do @show.first.question
it will give you question of first object.
And
If you do @show.map(&:question)
it will give you array of question.
To get all the information.
@show.each do |show|
puts show.question
puts show.question_status
puts show.user_id
end
You can optimize it as per you requirement.
Upvotes: 0
Reputation: 6542
Try this code at your rails console
@show = Question.where(:question_status => params[:id])
@show.each_with_index do |ques, index|
p "#{index+1} :: #{ques.question}"
end
With the help of index, you will get the sequence number of rows.
Upvotes: 0
Reputation: 3154
You want to take an array of all question fields?
questions = Question.where(:question_status => params[:id]).map(&:question)
Then you can simply go like
questions.each{|question| puts question }
Upvotes: 0
Reputation:
@show is an ActiveRecord::Relation object(Array). It does not belong to Question class.
@show = Question.where(:question_status => params([:id]).first
if you are expecting only one result. Otherwise you have to iterate the array for each element
Upvotes: 0