I'm sure this is a completely obvious beginner question, but trying to find answers to beginner Ruby questions on google is turning out to be an exercise in futility.
Anyway, suppose I have a database table that looks like this:
MyMessage
==================
int :id
string :from
string :to
string :messagetext
Now, if I need to expose a URL that takes in querystring params in the form of:
http://mysite.com/?sender=alice&receiver=bob&message=Hello
What is the best way to map the querystring params to my model?
Right now, I'm just doing it by brute force inside the controller:
@sender = params[:sender]
@receiver = params[:receiver]
@message = params[:message]
@mymessage = MyMessage.new
@mymessage.from = @sender
@mymessage.to = @receiver
@mymessage.messagetext = @message
@mymessage.save
My guess is that I should be able to create a class representing this type of querystring request, i.e.
class Request
attr_accessor :from
attr_accessor :to
attr_accessor :message
end
Then use some RoR magic to create the Request
object from the params
and then more magic to create a method that maps the Request
object to the MyMessage
object.
Any suggestions? Does this make sense at all?