Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>the Item model could be declared as polymorphic (se here: <a href="http://guides.rails.info/association_basics.html#polymorphic-associations" rel="nofollow noreferrer">http://guides.rails.info/association_basics.html#polymorphic-associations</a>)</p> <p>so you'll declare Item:</p> <pre><code>class Item &lt; ActiveRecord::Base belongs_to :category, :polymorphic =&gt; true end </code></pre> <p>then the 3 models that act as category (I'll show one, the others are same):</p> <pre><code> class Employee &lt; ActiveRecord::Base has_one :item, :as =&gt; :category end # and so on... </code></pre> <p>this way you can associate an Item to one of the 3 models (Book, Furniture and Electronics). each instance of these models will have an 'items' atribute, like this:</p> <pre><code>@book.items # returns all the items associated to a @book object </code></pre> <p>You can also use has_many association using a polymorphic model.</p> <p>For the views, you can use Nested Form Objects (see here: <a href="http://guides.rails.info/form_helpers.html" rel="nofollow noreferrer">http://guides.rails.info/form_helpers.html</a>). basically, in each form, you should nest a form to create an Item object. to follow the Book example, you'll have something like this:</p> <pre><code> &lt;%= form_for :book, @book do |form| %&gt; &lt;%= form.text_field :isbn %&gt; &lt;!-- other fields --&gt; &lt;%= fields_for @book.item do |item_form| %&gt; &lt;%= item_form.text_field :name %&gt; &lt;% end %&gt; &lt;% end %&gt; </code></pre> <p>hope this helped you ;)</p>
 

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