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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:03:48+00:00 2026-05-13T09:03:48+00:00

I need some help over here to understand how the model relationship works on

  • 0

I need some help over here to understand how the model relationship works on rails. Let me explain the scenario.

I have created 3 models.

  1. Properties
  2. Units
  3. Rents

Here is the how relationship mapped for them.

Model #property.rb

 class Property < ActiveRecord::Base
      has_many :units
      has_many :rents, :through=> :unit
    end

Model #unit.rb

class Unit < ActiveRecord::Base
  belongs_to :property
  has_many :rents
end

Model #rent.rb

class Rent < ActiveRecord::Base
  belongs_to :unit
end

here is the the schema

     create_table "units", :force => true do |t|
        t.integer  "property_id"
        t.string   "number"
        t.decimal  "monthly_rent"
      end

 create_table "rents", :force => true do |t|
        t.integer  "unit_id"
        t.string   "month"
        t.string   "year"
        t.integer  "user_id"
      end

OK, here is my problem. Let’s say I have 2 properties

  • property A
  • property B

and

  • property A has unit A01,A02,A03
  • property B has unit B01,B02,B03

I need to generate a report which shows the SUM of all the outstanding rents based on the property and month

So here is how it should be looks like. (tabular format)

  • Property A – December – RENT SUM GOES HERE
  • Property B – December – RENT SUM GOES HERE

So I got all the properties first. But I really can’t figure out a way to merge the properties and units (I guess we don’t need the rents model for this part) and print them in the view. Can someone help me to do this. Thanks

  def outstanding_by_properties

    @properties = Property.find(:all)
    @units = Unit.find(:all,:select=>"SUM(monthly_rent) as total,property_id",:conditions=>['property_id IN (?)',@properties])
  end
  • 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-13T09:03:48+00:00Added an answer on May 13, 2026 at 9:03 am

    I think something like this will work for you. Hopefully an SQL guru will come along and check my work. I’m assuming your Property model has a “name” field for “Property A,” etc.–you should change it to whatever your field is called.

    def outstanding_by_properties
      Property.all  :select => "properties.name, rents.month, SUM(units.monthly_rent) AS rent_sum",
                    :joins => { :units => :rents },
                    :group => "properties.id, rents.month, rents.year"
    end
    

    This should return an array of Property objects that have the attributes name, month, and rent_sum.

    It basically maps to the following SQL query:

    SELECT properties.name, rents.month, SUM(units.monthly_rent) AS rent_sum
    FROM properties
      JOIN units ON properties.id = units.property_id
      JOIN rents ON units.id = rents.unit_id
    GROUP BY properties.id, rents.month, rents.year
    

    The JOINs connect rows from all three tables and the GROUP BY makes it possible to do a SUM for each unique combination of property and month (we have to include year so that e.g. December 2008 is not grouped together with December 2009).

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

Sidebar

Ask A Question

Stats

  • Questions 253k
  • Answers 253k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Eric Petroelje is right, but I would build helper a… May 13, 2026 at 9:57 am
  • Editorial Team
    Editorial Team added an answer ok, i figured it out. there's no direct access to… May 13, 2026 at 9:57 am
  • Editorial Team
    Editorial Team added an answer You're missing a call to ReplayAll: _MockRepository.ReplayAll(); CollectionAssert.AreEqual(dataSeries, _DataSourceContext.FetchDataSeries(level, year,… May 13, 2026 at 9:57 am

Related Questions

I'm stuck here again. I have a database with over 120 000 coordinates that
I like to create a file full of custom functions which I have made,
I am looking for advice about how to parse a single list, using two
Im designing a game on visual C# the game must contain two text boxes
I'm slowly teaching myself cocoa for the iPhone(through the Stanford Class on iTunes U

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.