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

  • Home
  • SEARCH
  • 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 3807302
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T14:58:11+00:00 2026-05-19T14:58:11+00:00

Greetings I have 3 db tables: Types id name Sources id name type_id Operations

  • 0

Greetings
I have 3 db tables:

Types
id
name

Sources
id
name
type_id

Operations
id
source_id
comment
…

and models for each:

class Type < ActiveRecord::Base
  has_many :sources, :dependent => :destroy
end

class Source < ActiveRecord::Base
  belongs_to :type
  has_many :operations, :dependent => :destroy
end

class Operation < ActiveRecord::Base
  belongs_to :source
  default_scope :order => 'created_at DESC'
end

In Operation#index controller i have code for getting data (generated by scaffolding)

@operations = Operation.all

And piece of view operations/index.html.erb also generated by scaffolding

<% @operations.each do |operation| %>
  <tr>
    <td><%= operation.source_id %></td>
    <td><%= operation.comment %></td>
  </tr>
<% end %>

Now I want to use source.name instead of *operation.source_id*

I tried to do:

-replace operation.source_id to operation.sources.name # doesn’t work

-tried to using :joins, and can’t get Sources table fields

irb(main):057:0> Operation.first( :joins => :source )
=> #<Operation id: 2088, source_id: 1, summ: 10.0, comment: "", created_at: "2011-01-01 07:39:45", updated_at: nil>

or

irb(main):063:0> Operation.first( :joins => 'INNER JOIN sources ON operations.source_id = sources.id' )
=> #<Operation id: 2088, source_id: 1, summ: 10.0, comment: "", created_at: "2011-01-01 07:39:45", updated_at: nil>

How I must properly use :joins for get additional field?
Or there is another approach to get combined tables data.

And why in the operations/show.html.erb i can use <%= @operation.source.name %> and successfully get source.name, but in the *operations/index.html.er*b can’t

  • 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-05-19T14:58:12+00:00Added an answer on May 19, 2026 at 2:58 pm
    <% @operations.each do |operation| %>
      <tr>
        <td><%= operation.source.name %></td>
        <td><%= operation.comment %></td>
      </tr>
    <% end %>
    

    I’d also recommend changing your #index method to use an includes statement to avoid an N+1 situation (i.e. running a separate database query for each individual operation’s source).

    @operations = Operation.includes(:source).all
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Greetings, I have these 2 models: from django.db import models class Office(models.Model): name =
Greetings. I have files that contains eleven fields in the file name. Each file
Greetings, I have some mysql tables that are currently using an md5 hash as
Greetings I have a program that creates multiples instances of a class, runs the
greetings all i have a domain class named car and i have an object
Greetings I have this structure: <ul id=list class=tabs> <li class=list><a href=#tab1>Dados do Responsável</font></a></li> <li
I'm having trouble with my project. I have 2 models class UserPrefs(db.Model): user =
Greetings - I have a table of Articles and a table of Categories. An
Greetings, fellow coders! I have this table that contains categories and subcategories (actually I
Greetings friends, In my MySQL database, I have 'MAB' table which contains information about

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.