Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The design is strange a bit. If you need nesting like </p> <pre><code>ProgramCategory &gt; ProgramSubcategory &gt; Program </code></pre> <p>then you need</p> <pre><code>class Program &lt; ActiveRecord::Base belongs_to :program_subcategory end class ProgramSubcategory &lt; ActiveRecord::Base belongs_to :program_category has_many :programs end class ProgramCategory &lt; ActiveRecord::Base has_many :programs, :through =&gt; :program_subcategories has_many :program_subcategories end </code></pre> <p>This way when you create a program you can assign a subcategory to it. And this subcategory is already assigned to category, so you can access it via <code>program.program_subcategory.program_category</code></p> <p>And you do not need <code>program_category_id</code> foreign key in <code>programs</code> because program is not connected to category <em>directly</em>, but via subcategory.</p> <hr> <p><strong>UPDATE</strong></p> <blockquote> <ol> <li>Each Program has ONE Category.</li> <li>Each Program has ONE Subcategory.</li> <li>Each Category has MANY Subcategories</li> <li>Each Category has MANY Programs.</li> <li>Each Subcategory has ONE Category</li> <li>Each Subcategory has MANY Programs.</li> </ol> </blockquote> <p>Then I believe that my answer is still valid. You see, my structure is the same as your description except <code>Each Program has ONE Category</code> (because rails has no <code>belongs_to through</code>). you <code>has one</code> is actually <code>belongs_to</code> (because it can belong to only one).</p> <p>But as soon as <code>Each Program has ONE Subcategory</code> and <code>Each Subcategory has ONE Category</code> program's subcategory's category will be the ONLY program's category. You can have <code>p.program_category</code> by defining a method on a Program class:</p> <pre><code>def program_category program_subcategory.program_category end </code></pre> <hr> <p>Now for the part of </p> <blockquote> <p>I should be able to retrieve and also EDIT these:</p> <p>p.program_category</p> </blockquote> <p>Imagine you have a Program in subcategory Comedy from category Movies.</p> <p>You say you want to be able to EDIT programs category directly (if I understood correctly), like this:</p> <pre><code>p.program_category = ProgramCategory.find_by_name("Sports") </code></pre> <p>But what you expect to be program's subcategory then? As soon as Sports have many subcategories? Do you expect it to be blank?</p> <p>So in this design the only way to change program's category is to change program's subcategory:</p> <pre><code>p.program_subcategory = ProgramSubcategory.find_by_name("Tennis") </code></pre> <p>And this will manke program's category == Sports, because Tennis belongs to Sports.</p> <p><strong>Note:</strong> If you really want sometimes to change program's category directly, leaving its subcategory blank it requires another design of course. I do not think it is very difficult but it requires more work with less help from Rails AR Associations magic.</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