Note that there are some explanatory texts on larger screens.

plurals
  1. POreferencing attributes in models with belongs_to relationships through a nested namespace
    primarykey
    data
    text
    <p>Ok, so I thought I understood how the relationship specifications work in rails but I've been struggling with this for a day now.</p> <p>Some context, I have two models Cars &amp; Model Names (e.g. Impala, Charger, etc), where Cars are instances of Model Names, and Model Names really is nothing more than a lookup table of Model Names and some other model level attributes. The Model Name controller is nested within the admin namespace as only admins can CRUD Model Names. End users can add instances of cars to the Cars model.</p> <p>So, in routes.rb I have:</p> <pre><code> resources :cars namespace :admin do resources :model_names end </code></pre> <p>The Model's are defined as:</p> <pre><code>class Admin::ModelName &lt; ActiveRecord::Base end class Car &lt; ActiveRecord::Base belongs_to :admin_model_name end </code></pre> <p>The Migrations are:</p> <pre><code>class CreateCars &lt; ActiveRecord::Migration def self.up create_table :cars do |t| t.string :chassis_number t.string :description t.references :admin_model_name t.timestamps end end class CreateAdminModelNames &lt; ActiveRecord::Migration def self.up create_table :admin_model_names do |t| t.string :model t.integer :sort_index #...additional attributes removed t.timestamps end </code></pre> <p>The admin CRUD of ModelName all work great. The problem is in the Car views. I think I should be referencing a particular cars model name like such:</p> <pre><code>&lt;%= @car.admin_model_names.Model =&gt; </code></pre> <p>But I get the error:</p> <pre><code>undefined method `admin_model_names' for #&lt;Car:0x000001040e2478&gt; </code></pre> <p>I've tried the attr_accessible on the ModelNames model but to no avail. The underlying data is referenced correctly. I have also have HABTMT relationship between Cars &amp; Users and that all worked fine referencing each others attributes from the different entities views. But haven't been able to get this to work. Is it due to the nested resource &amp; admin control inheritance?</p> <p>The only referencing method I found that works is:</p> <pre><code> &lt;%= Admin::ModelName.find(@car.admin_model_name_id).model %&gt; </code></pre> <p>But this really seems to be too much code (and expense of a find) to get to that attribute. Is there a Rails way?</p> <p>Thanks in advance.</p> <p>Scott</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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