Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql Rails数据库查询_Mysql_Ruby On Rails_Ruby - Fatal编程技术网

Mysql Rails数据库查询

Mysql Rails数据库查询,mysql,ruby-on-rails,ruby,Mysql,Ruby On Rails,Ruby,我试图从数据库中收集信息,然后通过flash将这些信息传递给我的视图。不幸的是,这些信息的格式是ActiveRecord::Relation,而不是我可以读取的任何格式 控制器查询 @message = Message.where(:all).limit(4).order(id: :desc).only(:order,:where) flash[:response] = @message redirect_to (:back) 如果我做了类似的事情 @message =

我试图从数据库中收集信息,然后通过flash将这些信息传递给我的视图。不幸的是,这些信息的格式是ActiveRecord::Relation,而不是我可以读取的任何格式

控制器查询

@message = Message.where(:all).limit(4).order(id: :desc).only(:order,:where)    
    flash[:response] = @message
    redirect_to (:back)
如果我做了类似的事情

@message = Message.where(:all).limit(4).order(id: :desc).only(:order,:where)    
    flash[:response] = @message.first.mess
    redirect_to (:back)
为了尝试获取mess列中的第一个返回值,我得到了一个未定义的方法错误。我一直在试图找到一个教程,告诉我如何采取信息,一旦我的查询已经运行,但我没有太多的运气与铁路的教程最近。我很感激你们能给我的帮助。完成此操作后,我将尝试在视图端格式化4种不同的结果。

消息列表:

@messages = Message.order(id: :desc).limit(4)
此:仅:订单,:何处取消您的限制4为什么

@消息现在是activerecord关联,不适合输出。。。。因此,如果您有4条信息,您可以:

if @messages.any? # maybe no messages come out
    flash[:response] = @messages.map(&:mess).join('<br>') # add an html newline between them
end
redirect_to :back

因此,我对查询的理解是这样的——从Messages ORDER by ID DESC LIMIT 4中选择*。但是,我的查询没有返回任何内容。我得到了错误“undefined method”any?:对于nil:NilClass“我感觉好像理解了代码,但是我不明白为什么我没有从查询中获得任何结果,因为语法对我来说似乎很好。很抱歉,通过意外点击enter提交。您有消息类吗?是的,一个简单的单类Messagetrue validates:user,:presence=>true end在控制台中运行此命令:Message.orderid::desc.limit4,您得到了什么?