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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T02:16:15+00:00 2026-05-15T02:16:15+00:00

I am having trouble accessing the correct information in my rails model (which I

  • 0

I am having trouble accessing the correct information in my rails model (which I believe to be correct) The schema for my tables are

create_table :schools do |t|
    t.string :name
    t.timestamps
end  

create_table :variables do |t|
    t.string :name
    t.string :category
    t.timestamps
end

create_table :data do |t|
    t.string :entry
    t.decimal :rank, :scale => 3
    t.integer :school_id, :null => false
    t.integer :variable_id, :null => false
    t.timestamps
end

Model classes:

class Datum < ActiveRecord::Base
    belongs_to :school
    belongs_to :variable
end

class School < ActiveRecord::Base
    has_many :data
    has_many :variables, :through => :data
end

class Variable < ActiveRecord::Base
    has_many :data
    has_many :schools, :through => :data
end

here is my school show.html.erb page currently:

<h2> <%= @school.name %> </h2>

<table>
<% @school.variables.each  do |variable| %>
<tr><tr/>
<td><%= variable.name %></td>
<td><%= Datum.find(:first, :conditions => {:school_id => @school.id, :variable_id => variable.id}).entry %></td> 
<td><%= link_to 'Edit', edit_school_path(@school) %></td>
<td><%= link_to 'Back', schools_path %></td>
<% end %>
</table>  

It does what I want it to but it does wayy to many queries. I’m assuming I have to do eager loading but based on examples I found online I couldn’t figure out how to do this (I’m really new to rails). Does anyone have any ideas?

  • 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-15T02:16:15+00:00Added an answer on May 15, 2026 at 2:16 am

    Try this and see if you get the results you expect, as well as a reduced number of queries.

    Datum.find(:first, 
               :conditions => {:school_id => @school.id, :variable_id => variable.id}, 
               :include => [:school, :variable])
    

    Also, MVC principles dictate that you shouldn’t do the find in your view; instead, in the controller’s method (app/controllers/school_controller.rb, method show) do

    @data = Datum.find(....)
    

    and in the view:

    <%= @data.entry %>
    

    That way you don’t pollute your view with DB access concerns.

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

Sidebar

Related Questions

I'm having trouble accessing only one XML element that has the same name as
I'm having trouble accessing the some string fields in a COM interface. Calling the
I'm having some trouble importing and accessing a private key with the ASPNET user.
I am having trouble with IE7. I have a header, which is an IMG.
I am having trouble integrating two products, one of which is mine and they
I am having trouble accessing the data field. I receive the error: Databinding methods
I am having trouble accessing certain properties in an object that I receive as
I am having trouble in accessing an array within a structure. How can I
Having trouble linking the Stomp.framework into an iPhone SDK application. http://code.google.com/p/stompframework/ I follow the
Im having trouble using a .NET COM in vb6, It compiles ok and I

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.