title: Accessing attributes from different associated models rails 3
body: <p>I am looking to get a better understanding of Active Model/Record relationships and how to call attributes dependent upon where the attributes are located (models) and where I am calling them. So for example I can access the attribute dish_name from within the recipe controller like so</p>
  
  <pre><code> def all_recipes
  @recipes = Recipe.all
   end
  </code></pre>
  
  <p>In the view</p>
  
  <pre><code>&lt;% @recipes.each do |r| %&gt;
  &lt;%= r.dish_name %&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>Now say i want to access a recipe attribute from within my controller called worldrecipes and i have just written a method that returns all recipes with the same country. a country has many recipes as a relation</p>
  
  <p>So my method is</p>
  
  <pre><code>def self.top_countries
  joins(:recipes).
    select('countries.*, count(*) AS recipes_count').
    group('countries.id').
    order('recipes_count DESC')
   end
  </code></pre>
  
  <p>My controller </p>
  
  <pre><code>@worldrecipes = Country.where(:name =&gt; params[:name])
  </code></pre>
  
  <p>and view</p>
  
  <pre><code>&lt;% @worldrecipes.each do |r|  %&gt;
  &lt;%= r.name %&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>so accessing the country name attribute is easy as its in the country model and thats where my query results are being returned from (I think)...My question is how do i access the dish_name attribute from my recipe model to that links to the country name</p>
  
  <p>Hope that makes sense, does anyone have a guide on how to work this out or some golden rules for this</p>
  
  <p>Thank you</p>