简体   繁体   中英

ruby on rails undefined method

sharvil@sharvil:~/railstut/blog$ rails c

Loading development environment (Rails 4.2.1)
2.2.2 :001 > q=Question.all

  Question Load (0.5ms)  SELECT `questions`.* FROM `questions`
 => #<ActiveRecord::Relation [#<Question questions_id: 1, question: "What is this", almuni_almuni_id: 1, category_category_id: 1>]> 

2.2.2 :002 > q

 => #<ActiveRecord::Relation [#<Question questions_id: 1, question: "What is this", almuni_almuni_id: 1, category_category_id: 1>]> 

2.2.2 :003 > q.question

NoMethodError: undefined method `question' for #<Question::ActiveRecord_Relation:0x000000045dc178>
    from /home/sharvil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.1/lib/active_record/relation/delegation.rb:136:in `method_missing'

I am getting a error in accessing q.questions .. question is column in table.

pls help.

You need to do - q.first.question . Your q is a collection of questions not an instance of Question . And you called question method on the collection of questions, that's why you got the error.

In your case q is question collection

q.each do |que|
  puts que.question
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM