Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to access associated model through another model in Rails?
    text
    copied!<p>UPDATE (specific and more detailed previous version is below):</p> <p>I'm developing a TV station web site. Here are requirements for my Program section:</p> <ol> <li>Each <code>Program</code> has ONE <code>Category</code>.</li> <li>Each <code>Program</code> has ONE <code>Subcategory</code>.</li> <li>Each <code>Category</code> has MANY <code>Subcategories</code></li> <li>Each <code>Category</code> has MANY <code>Programs</code>.</li> <li>Each <code>Subcategory</code> has ONE <code>Category</code></li> <li>Each <code>Subcategory</code> has MANY <code>Program</code>s.</li> </ol> <p>I want to retrieve all these three models to be associated. For example, I should be able to retrieve below data from my views:</p> <p>While:</p> <pre><code>p = Program.find(1) p_cat = ProgramCategory.find(1) p_subcat = ProgramSubcategory.find(1) </code></pre> <p>I should be able to retrieve and also EDIT these:</p> <pre><code>p.program_category p.program_subcategory </code></pre> <p>or</p> <pre><code>program_category.programs program_subcategory.programs </code></pre> <p>You can see what I tried below to achieve these requirements. You may recommend me a totally different way or fix my mistakes.</p> <p>Thank you</p> <p>============================================================</p> <p>I have 3 models. They are supposed to be nested in eachother.</p> <blockquote> <p>ProgramCategory > ProgramSubcategory > Program</p> </blockquote> <p>Here are my codes:</p> <p>ProgramCategory model:</p> <pre><code> has_many :programs has_many :program_subcategories </code></pre> <p>ProgramSubcategory model:</p> <pre><code>belongs_to :program_category has_many :programs </code></pre> <p>Program Model:</p> <pre><code>belongs_to :program_category belongs_to :program_subcategory </code></pre> <p>As I create a new Program, I can set its Category and everything is fine. I can access them from both sides. For example,</p> <pre><code>program.program_category </code></pre> <p>gives me what I expected. and also</p> <pre><code>program_category.programs </code></pre> <p>gives me what I want to have, too.</p> <p>BUT, -<em>here comes the question</em>-</p> <p>When I try to access <code>program.program_subcategory</code>, I receive just a nil.</p> <p>Eventhough my Subcategory's Category is set and my Program's Category is set too, why I can't access <code>program.program_subcategory</code> directly?</p> <p>When I type <code>program_category.program_subcategories</code>, I receive all Subcategories owned by that Category. But I CAN NOT get Subcategories from directly a <code>Program</code> object.</p> <p>My scheme is below. Any help is appriciated.</p> <pre><code>ActiveRecord::Schema.define(:version =&gt; 20120926181819) do create_table "program_categories", :force =&gt; true do |t| t.string "title" t.text "content" t.datetime "created_at", :null =&gt; false t.datetime "updated_at", :null =&gt; false end create_table "program_subcategories", :force =&gt; true do |t| t.integer "program_category_id" t.string "title" t.text "content" t.datetime "created_at", :null =&gt; false t.datetime "updated_at", :null =&gt; false end add_index "program_subcategories", ["program_category_id"], :name =&gt; "index_program_subcategories_on_program_category_id" create_table "programs", :force =&gt; true do |t| t.integer "program_category_id" t.integer "program_subcategory_id" t.string "title" t.text "content" t.datetime "created_at", :null =&gt; false t.datetime "updated_at", :null =&gt; false end add_index "programs", ["program_category_id", "program_subcategory_id"], :name =&gt; "my_join1", :unique =&gt; true end </code></pre>
 

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