开发者

Cannot access data in active record using dot notation

I created a model in Ruby and am stuck on a n00b issue. In Rails Console:

s = Survey.where(:keyword => 'foo') => [#] s.inittxtmsg NoMethodError: undefined method inittxtmsg' for #<ActiveRecord::Relation:0x10350f8f8> from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.5/lib/active_record/relation.r开发者_运维问答b:371:inmethod_missing' from (irb):3

Shouldn't I be able to see the values by typing s.Survey_id, s.inittxtmsg, s.keyword, s.store?

Thank you!


Survey.where(:keyword => 'foo')

returns an array of results, so you are really calling .inittxtmsg on an array, which obviously doesn't exist.

You could do something like:

Survey.where(:keyword => 'foo').first.inittxtmsg, in which it is calling it on the actual model object.

Or if you know that there is only one survey with the keyword = foo... you can use the find method to only return a single model object:

s = Survery.find_by_keyword("foo")
s.inittxtmsg
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜