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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:41:10+00:00 2026-05-31T02:41:10+00:00

I have a couple of models that are composites of multiple objects. I basically

  • 0

I have a couple of models that are composites of multiple objects. I basically manage them manually for saves and updates. However, when I select items out, I don’t have access to the associated properties of said item. For example:

class ObjectConnection < ActiveRecord::Base
  def self.get_three_by_location_id location_id
    l=ObjectConnection.find_all_by_location_id(location_id).first(3)
    r=[]
    l.each_with_index do |value, key|
      value[:engine_item]=Item.find(value.engine_id)
      value[:chassis_item]=Item.find(value.chassis_id)
      r << value
    end
    return r
  end
end

and each item:

class Item < ActiveRecord::Base
  has_many :assets, :as => :assetable, :dependent => :destroy

When I use the ObjectLocation.find_three_by_location_id, I don’t have access to assets whereas if use Item.find(id) in most other situations, I do.

I tried using includes but that didn’t seem to do it.

thx

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

    Sounds like the simplest solution would be to add methods to your ObjectConnection model for easy access like so:

    class ObjectConnection < ActiveRecord::Base
    
      def engine
        Engine.find(engine_id)
      end
    
      def chassis
        Chassis.find(chassis_id)
      end
    
      # rest of class omitted...
    

    I’m not exactly sure what you’re asking… If this doesn’t answer what you’re asking, then can you try to be a little bit more clear with what exactly you are trying to accomplish? Are the Chassis and Engine mdoels supposed to be polymorphic associations with your Item model?

    Also, the code you’re using above won’t work due to the fact that you are trying to dynamically set properties on a model. It’s not your calls to Item.find that are failing, it’s your calls to value[:engine_item]= and value[:chassis_item] that are failing. You would need to modify it to be something like this if you wanted to keep that flow:

    def self.get_three_by_location_id location_id
      l=ObjectConnection.find_all_by_location_id(location_id).first(3)
      r=[]
      l.each_with_index do |obj_conn, key|
        # at this point, obj_conn is an ActiveRecord object class, you can't dynamically set attributes on it at this point
        value = obj_conn.attributes # returns the attributes of the ObjectConnection as a hash where you can then add additional key/value pairs like on the next 2 lines
        value[:engine_item]=Item.find(value.engine_id)
        value[:chassis_item]=Item.find(value.chassis_id)
        r << value
      end
      r
    end
    

    But I still think that this whole method seems unnecessary due to the fact that if you setup proper associations on your ObjectConnection model to begin with, then you don’t need to go and try to handle the associations manually like you’re attempting to do here.

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

Sidebar

Related Questions

I have multiple models that need to have their history kept pretty much indefinitely
I have a couple of simple objects that have a many-to-many relationship. Django has
I have a couple of simple models that are associated like so: MODELS class
I have a couple of models in a Rails (2.3.11) application that are built
I have a couple of models that are both password centric models. They don't
I have a number of models that should be finished in a couple of
I have a couple of models in a Rails app. Let's say it's Products
I have a couple of methods I'd like to share between models rather than
I have couple resource DLLs that I currently load when application starts using following
I have couple of issues- the first constraint on select is loaded when reference

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.