0

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.

4
  • which database r u using? Commented Jun 5, 2013 at 6:39
  • if you asking for client...mysql Commented Jun 5, 2013 at 6:42
  • you want to take array of all question fields of Question table? See my answer. Commented Jun 5, 2013 at 6:52
  • Just try my solution, may it will help Commented Jun 5, 2013 at 6:54

5 Answers 5

1
 @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
Sign up to request clarification or add additional context in comments.

1 Comment

check again by show = Question.find_by_question_status(params[:id]) and show.question
0

@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

3 Comments

find method can search only by id or somthing else ?
sorry. thought he was searching for id when he passed id in params. see my edited answer
it give me only the last item in array which in question of this post
0

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 }

2 Comments

yes and other information. Can I use map(&:question) like this map(&:question, &status) ?
No you can't use like that. U want to take the status field also..?
0

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.

Comments

0

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.

3 Comments

I want get array information and then get from them all what i want, for example: question and status fields or only question field..
yes...it give me any field...but only for ONE item in array. When I searching by params[:id] - it is id of category and in array i get all information from this category. Your solution give me only ONE item.
i think i should use each in each

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.