Search code examples
ruby-on-rails-3activerecordactivemodel

Accessing attributes from different associated models rails 3


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

 def all_recipes
@recipes = Recipe.all
 end

In the view

<% @recipes.each do |r| %>
<%= r.dish_name %>
<% end %>

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

So my method is

def self.top_countries
joins(:recipes).
  select('countries.*, count(*) AS recipes_count').
  group('countries.id').
  order('recipes_count DESC')
 end

My controller

@worldrecipes = Country.where(:name => params[:name])

and view

<% @worldrecipes.each do |r|  %>
<%= r.name %>
<% end %>

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

Hope that makes sense, does anyone have a guide on how to work this out or some golden rules for this

Thank you


Solution

  • For starters you want to make sure you have the association setup in your models:

    country.rb

    class Country < ActiveRecord::Base  
      has_many :recipes
    end
    

    recipe.rb

    class Recipe < ActiveRecord::Base  
      belongs_to :country
    end
    

    If you haven't already done so, add a foreign_key attribute to your recipe model by running the following migration:

    rails g migration add_country_id_to_recipe country_id:integer
    

    Now that your associations are in place you can easily query for a countries respective recipes. In your controller:

    @worldrecipes = Country.where(:name => params[:name])
    

    Then in your view:

    <% @worldrecipes.each do |c|  %>
      <% c.recipes.each do |r| %>
       <%= r.dish_name %>
      <% end %>
    <% end %>
    

    In regards to 'golden rules' I highly recommend you check out Association Basics. This is the go-to place for an overview of what you can do with associations.