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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:16:39+00:00 2026-05-26T00:16:39+00:00

class Box embeds_many :things after_init :add_default_things def add_default_things self.things.build(title: Socks, value: 2) self.things.build(title: Ring,

  • 0
class Box
  embeds_many :things
  after_init :add_default_things

  def add_default_things
    self.things.build(title: "Socks", value: 2)
    self.things.build(title: "Ring", value: 1)
  end
end

class Thing
  field :title
  field :value, type: Integer
end

all boxes have got some default things: Socks and a Ring. Everybody can add this or another things into a box. So now I need to order all boxes by count of socks:

Box.order_by(:thing_with_title_socks.value.desc) # ???
  • 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-26T00:16:40+00:00Added an answer on May 26, 2026 at 12:16 am

    I am not good at ruby so I am going to try to explain it in java terms. The problem is how you design your data. For example you have n Box classes, each having a list of items in it:

    public class Box {
      public List<Item> items;
    }
    
    public class Item {
      public String name;
      public int value;
    }
    

    to sort all boxes in this classes for a specific item value you need to loop through all items in boxes which is not good at all. so I would change my code to do it more efficiently:

    public class Box {
      public Map<Integer, Item> items;
    }
    
    public class Item {
      public int id;
      public String name;
      public int value;
    }
    

    this way I would check if the item exists in box or not; access the items value with O(1) complexity, and I wouldn’t mistakenly put 2 different packets of socks in my box which would cause an error (which one would it sort for?).

    your current data structure is like this :

    {
      things : [ {"name" : "socks", "value" : 2} , {"name" : "ring", "value" : 5} ]
    }
    

    but if you are going to do sorting / direct access (using in query) on “known” object (like ring or socks) then your data should look like:

    {
      things : { "socks": 2, "ring" : 5 }
    }
    

    if you have extra information attached to this items they can also look like :

    {
      things : { "S01" : { "name" : "super socks", "value" : 1 }, "S02" : { "name" : "different socks", "value" : 2} }
    }
    

    this way you have direct access to the items in box.

    hope this would help.

    Edit : I thought it was obvious but just to make thing clear: you can’t efficiently query “listed” child data unless you know the exact position. (You can always use map/reduce though)

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

Sidebar

Related Questions

Say I have a class Box with two attributes, self.contents and self.number. I have
$('div.box').html(hello) puts the word hello inside all my div's of class 'box'. They all
<?php class Box { var $contents; function Box($contents) { $this-&gt;contents = $contents; } function
I know it's possible to create a friend function in C++: class box {
Is there any html form class(text box) that does not allow null values. The
I'm trying to tar up all the *.class files only on a Solaris box
I have a test class and a box class, in the test class i
I want after keyup on input change background-color input into class box , but
I have this class .box { background-color:#fff; border:3px solid #eee; } My question is
Consider the following: public class Box { public BoxSize Size { get; set; }

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.