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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:21:52+00:00 2026-06-04T16:21:52+00:00

What would be the right (Rails) way to have one model with both a

  • 0

What would be the right (Rails) way to have one model with both a has_many and a has_one relationship? In my case, I want my Device model to keep track of both its current location and all of its previous locations.
This is my attempt and it is functional but is there a better way?

Models

class Location < ActiveRecord::Base
  belongs_to :device
end

class Device < ActiveRecord::Base
  has_many :locations # all previous locations
  belongs_to :location # current location
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-06-04T16:21:54+00:00Added an answer on June 4, 2026 at 4:21 pm
    class Location < ActiveRecord::Base
      belongs_to :device
    end
    
    class Device < ActiveRecord::Base
      has_many  :locations
    
      def previous_locations
        self.locations.order('created_at asc').limit( self.locations.count-1)
      end
    
      def current_location # or last_location
        self.locations.order('created_at desc').limit(1)
      end
    
      # you may like to add this one
      def current_location= args
        args = Location.new args unless args.is_a? Location
        self.locations << args
      end
    end
    

    Note that all @device.locations, @device.previous_locations, and @device.current_location will return ActiveRecord::Relation

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

Sidebar

Related Questions

Right now I have a Rails 3 model for storing profile data. One of
I've been struggling with the right way to design a Rails model/controller for a
I currently have a model called Album which has_many :songs . I would like
I would like to know what is the right way to copy two-dimension array
How would I have a top right corner div as shown in the image.
I would like to have the page on user/sidebar on the right in the
Okay I have an extensive ruby application running behind Ruby on Rails right now.
In a Rails 3.2 app I have a model Project, which has many Tasks.
Using Rails 3.0.7. I have a simple form which shows all records from one
I'm using Rails 3.1. I have a model Tree and a model TreeNode, and

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.