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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:12:30+00:00 2026-05-29T05:12:30+00:00

Quite experienced but self taught with PHP/MySQL and playing about with Rails. Really love

  • 0

Quite experienced but self taught with PHP/MySQL and playing about with Rails. Really love what i’ve done with it do so far but am seriously struggling with what in my mind should be really simple; joining data from various tables together and then displaying it in various views. I think I get the concept of Active Record, I just can’t seem to get it to work they way I want.

I’m working on building a fairly involved music cataloging system ala Discogs, with artist, release, product, track and label levels and have the following models:

class Artist < ActiveRecord::Base
  has_and_belongs_to_many :releases
end

class Release < ActiveRecord::Base
has_many :products
end

class Product < ActiveRecord::Base
belongs_to :release
has_many :tracks
end

class Tracks < ActiveRecord::Base
has_and_belongs_to_many :products
end

Eventually, I want to introduce territories, sales and other data but until I nail the basics above I can’t move fwd.

In my products controller I have:

@product = Product.find(:all, :include => :release)

Which produces:

Processing by ProductsController#index as HTML
Product Load (0.1ms)  SELECT "products".* FROM "products" 
Release Load (0.2ms)  SELECT "releases".* FROM "releases" WHERE "releases"."id" IN (1, 2, 3, 10, 4)

But I am thinking this might be unnecessary if my associations are correct?

In my products view for example I want to list products with the title pulled in from the releases table. I’ve tried the following but get a ‘NoMethodError’ on title:

<% @product.each do |product| %>

<tr>    
<td><%= product.title %></td>
<td><%= product.cat_no %></td>
<td><%= product.barcode %></td>
<td></td>

</tr>
<% end %>

Help!!

Massive thanks in advance.

Ryan

  • 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-29T05:12:30+00:00Added an answer on May 29, 2026 at 5:12 am
    Release Load (0.2ms)  SELECT "releases".* FROM "releases" WHERE "releases"."id" IN (1, 2, 3, 10, 4)
    

    The above query is generated because you have used :include=>:association(:release in your case) in find method.This the property of Rails is called as eager loading widely used for performance optimization.There is no problem in your association I think.

    If you have used

    @products = Product.all

    And in your view page

    <% @product.each do |product| %>
     <tr>    
      <td><%= product.title %></td>
      <td><%= product.cat_no %></td>
      <td><%= product.barcode %></td>
      <td><%= product.release.name %></td>
     </tr>
    <% end %>
    

    The method call product.release will create the above query for the ‘n’ number of times the loop executed.(n database hits)

    If you use :include option this can be avoided.Rails preloads the related association(:release).(only 1 databse hit)

    For more details visit

    http://guides.rubyonrails.org/active_record_querying.html

    http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html

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

Sidebar

Related Questions

I'm quite experienced in PHP but I don't quite use mod_rewrite (although I should).
Hey, I'm quite experienced with PHP but I have no idea what the keyword
I'm a starter with Emacs (but quite experienced Vim user) and trying to play
I've used BackgroundWorkers quite a bit, but I've never experienced this problem before. My
I am (quite) an experienced programmer but totally new to Ruby and Ruby on
I'm quite experienced in XAML/WPF but am new to Flex/Flex4. I'm trying to build
I'm rather new to Ant but I have experienced it's quite good pattern to
I'm an experienced programmer, but a novice to XSLT and am finding it quite
I am quite experienced with coding in Javascript, but there's still one thing I
I'm a pretty experienced Java programmer that's been doing quite a bit of Win32

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.