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 8197077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:40:04+00:00 2026-06-07T05:40:04+00:00

When i try to access to a method created by the relationship has_many –

  • 0

When i try to access to a method created by the relationship has_many – belogs_to, i get an undefined method error . Here is the code (semplified) :

local.rb (model) :

class Local < ActiveRecord::Base
   has_many :foods, dependent: :destroy, :foreign_key => 'local_id'
end

food.rb :

class Food < ActiveRecord::Base
   attr_accessible :descrizione, :nome, :prezzo, :voto
   belongs_to :local, :foreign_key => 'local_id'
end

the /locals/show.html.erb view that calls the create action on foodsController :

...

<%= form_for :food, :url => {:controller  => :foods, :action => :create} do |f| %>
...
<% end %>

foods_controller.rb

class FoodsController < ApplicationController
    def create
        @food = @local.foods.build(params[:food])
    end
end

The problem is raised here in foods_controller on create action : “undefined method ‘foods’ ‘ . Why? the relationship should enable the local.foods method?
(@local is defined in locals_controller.rb, @local = Local.find(params[:id]) , and local_id attribute is defined in schema.rb and in the last db migration , just to be clear)

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-07T05:40:05+00:00Added an answer on June 7, 2026 at 5:40 am

    Since @local is defined in a different controller, you won’t be able to access it from FoodController.

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

Sidebar

Related Questions

I created a Java code that tries to access the method LoadProject of the
When try to access site i created after 3 login attempts it gives unauthorised
Every time I try to access my localhost/phpmyadmin it gives me this error --->
The error message I gen when I try to access the web page server
I try to access session variables stored on a different page. When I have
I try to access the MainContentBlock control from the aspx, but unable to do
When I try to access my site, then check my Heroku logs, I see
When you try to access a key which isn't in a Dictionary (for example),
When I try to access info that is not presented in xml like so:
We know that if we try to access a nonexistent key of std::map with

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.