Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is a two part railscast covering aspects of implementing a poll/survey application. It covers most of your model related doubts.</p> <p><a href="http://railscasts.com/episodes/196-nested-model-form-part-1" rel="nofollow">http://railscasts.com/episodes/196-nested-model-form-part-1</a></p> <p><a href="http://railscasts.com/episodes/197-nested-model-form-part-2" rel="nofollow">http://railscasts.com/episodes/197-nested-model-form-part-2</a></p> <p>I would create the dependent objects during assignment by over-riding the setter for <code>text_body</code>.</p> <p>Eg:</p> <pre><code>def text_body=(val) write_attribute(:text_body, val).tap do |v| append_new_tags_and_mentions end end def append_new_tags_and_mentions tag_list, mention_list = extract_tags_and_mentions new_mentions = mention_list - mentions.where(name =&gt; mention_list).pluck(:name) mentions.concat(*new_mentions) if new_mentions.present? new_tags = tag_list - hashtags.where(tag =&gt; tag_list).pluck(:tag) hashtags.concat(*new_tags) if new_tags.present? end def extract_tags_and_mentions tag_list = [] mention_list = [] text_body.scan(ENTITY_PATTERN) do |boundary, token, value| if token == "@" mention_list &lt;&lt; value else tag_list &lt;&lt; value end end [tag_list, mention_list] end </code></pre> <p>Add a validator to check the dependencies.</p> <p>General guideline I wish I knew before I started programming in rails after working in Java/C++/SQL for a long time.</p> <ul> <li><p>Do not hand code the table generation SQL</p></li> <li><p>Use db:create rake tasks for creating tables</p></li> <li><p>Rails does not support foregin keys. You can enforce it through validators.</p></li> <li><p>Do not use semicolons to terminate the line. One of the joys of ruby is you don't have terminate lines.</p></li> <li><p>Do not use explicit hashes for DSL API parameters.</p> <p>Use this idiom</p> <pre><code>belongs_to :content, :polymorphic =&gt; true </code></pre> <p>Instead of:</p> <pre><code>belongs_to :content, { :polymorphic =&gt; true }; </code></pre></li> <li><p>Use modules instead of inheritance for code re-usage.</p></li> <li><p>Use <code>each</code> instead of <code>for</code></p></li> <li><p>Learn <code>map</code>, <code>reduce</code> (i.e. <code>inject</code>) functions on an array.</p></li> </ul>
    singulars
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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