Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You should not expect to be able to generate your app via scaffolding alone. It is meant only to provide an example for getting started.</p> <p>The most flexible kind of many-to-many relationship in rails is called <a href="http://guides.rubyonrails.org/association_basics.html#the-has_many-through-association" rel="nofollow noreferrer">has many through</a>. This requires a join table which would typically be called 'categorisations' in this case. It would need a <code>product_id</code> column declared as <code>belongs to :product</code> and a <code>category_id</code> column declared as <code>belongs_to :category</code>. The three models (including the join model) would be declared thus:</p> <pre><code># Table name: products # Columns: # name:string class Product &lt; ActiveRecord::Base has_many :categorisations, dependent: :destroy has_many :categories, through: :categorisations end # Table name: categories # Columns: # name:string class Category &lt; ActiveRecord::Base has_many :categorisations, dependent: :destroy has_many :products, through: :categorisations end # Table name: categorisations # Columns: # product_id:integer # category_id:integer class Categorisation &lt; ActiveRecord::Base belongs_to :product belongs_to :category end </code></pre> <p>Note that I've named the columns <code>name</code> rather than <code>prd_name</code> since this is both human-readable and avoids redundant repetition of the table name. This is highly recommended when using rails.</p> <p>The models can be generated like this:</p> <pre><code>rails generate model product name rails generate model category name rails generate model categorisation product:references category:references </code></pre> <p>As for generating the scaffolding, you could replace <code>model</code> with <code>scaffold</code> in the first two commands. Again though, I don't recommend it except as a way to see an example to learn from.</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.
    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