Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8977331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:20:46+00:00 2026-06-15T19:20:46+00:00

I am looking to get a better understanding of Active Model/Record relationships and how

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T19:20:47+00:00Added an answer on June 15, 2026 at 7:20 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Looking for an better understanding of how an mvc project should define javascript and
Im just looking for a little clarity to better my understanding of the Rails
I'm looking for a high-level, algorithmic understanding so that I can get a Big-O
I am trying to get a better understanding of some of the inner workings
I am looking to get my MCTS then a MCPD. I learn better by
I am trying to write some backbone.js stuff to get a better understanding on
I'm looking to get a more thorough understanding of the ASP.NET page lifecycle. I'm
When looking to get data from a web page whats the recommended method if
I'm looking to get a Regex for the following password strength requirements: Minimum 8
I'm looking to get an instance of Solr search running on my localhost. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.