Note that there are some explanatory texts on larger screens.

plurals
  1. PODatabase nesting model layout confusion
    text
    copied!<p>I'm no expert in databases and a beginner in Rails, so here goes something which kinda confuses me...</p> <p>Assuming I have three classes as a sample (note that no effort has been made to address any possible Rails reserved words issue in the sample).</p> <pre><code>class File &lt; ActiveRecord::Base has_many :records, :dependent =&gt; :destroy accepts_nested_attributes_for :records, :allow_destroy =&gt; true end class Record &lt; ActiveRecord::Base belongs_to :file has_many :users, :dependent =&gt; :destroy accepts_nested_attributes_for :users, :allow_destroy =&gt; true end class User &lt; ActiveRecord::Base belongs_to :record end </code></pre> <p>Upon entering records, the database contents will appear as such. My issue is that if there are a lot of Files for the same Record, there will be duplicate record names. This will also be true if there will be multiple Records for the same user in the the Users table.</p> <p>I was wondering if there is a better way than this so as to have one or more files point to a single Record entry and one or more Records will point to a single User. BTW, the File names are unique.</p> <p>Files table:</p> <pre><code>id name 1 name1 2 name2 3 name3 4 name4 </code></pre> <p>Records table:</p> <pre><code>id file_id record_name record_type 1 1 ForDaisy1 ... 2 2 ForDonald1 ... 3 3 ForDonald2 ... 4 4 ForDaisy1 ... </code></pre> <p>Users table:</p> <pre><code>id record_id username 1 1 Daisy 2 2 Donald 3 3 Donald 4 4 Daisy </code></pre> <p>Is there any way to optimize the database to prevent duplication of entries, or this should really the correct and proper behavior. I spread out the database into different tables to be able to easily add new columns in the future.</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