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.
@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.
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