Rails Database Query

I am trying to gather information from a database and then pass that information on to my view via flash. Unfortunately the information is formatting in an ActiveRecord::Relation and not in any format that I can read.

Controller Query

@message = Message.where(:all).limit(4).order(id: :desc).only(:order,:where)    
    flash[:response] = @message
    redirect_to (:back)

If I do something like

@message = Message.where(:all).limit(4).order(id: :desc).only(:order,:where)    
    flash[:response] = @message.first.mess
    redirect_to (:back)

To try and get the first returned value in the mess column, I get an undefined method error. I have been trying to find a tutorial that tells me how to take information once my query has been run but I have not had much luck with Rail's tutorials as of late. I appreciate any help that you guys can give. After I do this I am going to try to format the 4 different results on the view side.

Answers


List of messages:

@messages = Message.order(id: :desc).limit(4)

This: only(:order,:where) cancels your limit(4) (why?)

@messages is now an activerecord association, not suitable to output.... So, if you have 4 messages top you can do:

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

Need Your Help

Redirecting the output of a child process

c shell unix io-redirection

There are several ways of redirecting the output of a child process:

Jenkins /var/log/jenkins takes 7GB in two weeks

logging jenkins diskspace

After about two weeks of usage I have found out that

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.