Note that there are some explanatory texts on larger screens.

plurals
  1. PORuby Custom Class to Model
    primarykey
    data
    text
    <p>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.</p> <p>Anyway, suppose I have a database table that looks like this:</p> <pre><code>MyMessage ================== int :id string :from string :to string :messagetext </code></pre> <p>Now, if I need to expose a URL that takes in querystring params in the form of:</p> <blockquote> <p><a href="http://mysite.com/?sender=alice&amp;receiver=bob&amp;message=Hello" rel="nofollow noreferrer">http://mysite.com/?sender=alice&amp;receiver=bob&amp;message=Hello</a></p> </blockquote> <p>What is the best way to map the querystring params to my model?</p> <p>Right now, I'm just doing it by brute force inside the controller:</p> <pre><code>@sender = params[:sender] @receiver = params[:receiver] @message = params[:message] @mymessage = MyMessage.new @mymessage.from = @sender @mymessage.to = @receiver @mymessage.messagetext = @message @mymessage.save </code></pre> <p>My guess is that I should be able to create a class representing this type of querystring request, i.e.</p> <pre><code>class Request attr_accessor :from attr_accessor :to attr_accessor :message end </code></pre> <p>Then use some RoR magic to create the <code>Request</code> object from the <code>params</code> and then more magic to create a method that maps the <code>Request</code> object to the <code>MyMessage</code> object.</p> <p>Any suggestions? Does this make sense at all?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload