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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:16:08+00:00 2026-06-03T22:16:08+00:00

We have 3 models model1, model2, model3. This 3 are connect with class Model1

  • 0

We have 3 models model1, model2, model3. This 3 are connect with

class Model1 < ActiveRecord::Base
  has_many :model2s
end
class Model2 < ActiveRecord::Base
  belongs_to :model1s
  has_many :model3s
end
class Model3 < ActiveRecord::Base
  belongs_to :model2s
end

In the table of Model1 exists id.

In the table of Model2 exists id and model1_id.

In the table of Model3 exists id and model2_id.

Now i want to get Data from the table1 and table2 out of Model3Controller.
This works fine.

class Model3Controller < ApplicationController
  def create
    @model2controller = Model2.find(params[:model3controller][:model2_id])
    @model1controller = ?
  end
end

How do we get data from the associated dataset from the table1 of the model1controller? Did we have to add the model1_id into the table3, or can we call it on an other way. Like this pseudocode

@model1controller = Model1.find(
params[:model3controller][:model2_id]params[:model2controller][:model1_id])
  • 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-03T22:16:10+00:00Added an answer on June 3, 2026 at 10:16 pm

    I might be missing something, but it looks like you should only do the following:

    model1 = Model3.find_by_id(params[:model3_id]).model2.model1
    

    Edit. The above code works just like the following lines:

    model3 = Model3.find_by_id(params[:model3_id]) # instantiate a model3
    
    model2 = model3.model2 # from mode3 grab a model2
                           # this is possible due to the fact that model3 is associated with model2 via the belongs_to relation
    
    model1 = model2.model1 # model2 "belongs_to" model1, i.e. we can grab model1 by asking model2: "whom do belong to?" just like in the previous example
    

    Please note that accessing methods on instance is done calling in lowercase, i.e.

    object.method # => something is returned
    

    In your example you make mistake, calling Model3.find_by_id(params[:id]).Model2 (notice the capital letter in Model2, which is wrong)

    Also, check if params[:id] are actually being passed to the controller action. Otherwise, if it’s absent, Model3.find_by_id(params[:id]) will return nil and thus a mentioned error will be raised.

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

Sidebar

Related Questions

I have models like this: class IdfPracownicy(models.Model): nazwa = models.CharField(max_length=100) class IdfPracaOpinie(models.Model): nazwa =
I have two models like this: class OptionsAndFeatures(models.Model): options = models.TextField(blank=True, null=True) entertainment =
I have this model in django: class JournalsGeneral(models.Model): jid = models.AutoField(primary_key=True) code = models.CharField(Code,
I have a simple model like this one: class Artist(models.Model): surname = models.CharField(max_length=200) name
Newbie question. I have Django models that look like this: class Video(models.Model): uploaded_by =
I have a model class like this: class Note(models.Model): author = models.ForeignKey(User, related_name='notes') content
I have a model.py like this: class EventTypeCategory(models.Model): name = models.CharField(max_length=50, verbose_name=Name) user =
I have a model that is something like this: class Input(models.Model): details = models.CharField(max_length=1000)
I have a model that looks like this: class Invite(models.Model): user = models.ForeignKey(User) event
I have a report model looking a bit like this: class Report(models.Model): date =

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.