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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:18:56+00:00 2026-05-11T20:18:56+00:00

In an ActiveRecord (CakePHP flavored) setup I have a HasAndBelongsToMany association setup on Videos

  • 0

In an ActiveRecord (CakePHP flavored) setup I have a HasAndBelongsToMany association setup on Videos and Bins: A Bin can store n references to Videos, and Videos can belong to n Bins.

I need to be able to manually set and store the display order of the Videos within a particular Bin (so the client can have his Videos in a particular order within the Bin.) My initial thought is to create a field in Bin that stores an array of Video IDs in the order they need to appear. Is this the most efficient way to handle this?

If so, when I then get the HABTM query result, what is the cleanest/most efficient way to re-order the returned query to match the sorted array of ID’s?

The Videos associated with a Bin are returned as an array:

[Video] => Array
            (
                [0] => Array
                    (
                        [id] => 49b2de95-0338-48b9-bc88-01ba35664358
                        ...

                    )

                [1] => Array
                    (
                        [id] => 49b1ccad-b770-410e-be46-03a035664358
                        ...
            )

Or is there a better way to achieve what I’m trying to do without using HABTM associations?

Thanks in advance –

  • 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-11T20:18:56+00:00Added an answer on May 11, 2026 at 8:18 pm

    What to do when HABTM becomes complicated?

    By default when saving a HasAndBelongsToMany relationship, Cake will delete all rows on the join table before saving new ones. For example if you have a Club that has 10 Children associated. You then update the Club with 2 children. The Club will only have 2 Children, not 12.

    Also note that if you want to add more fields to the join (when it was created or meta information) this is possible with HABTM join tables, but it is important to understand that you have an easy option.

    HasAndBelongsToMany between two models is in reality shorthand for three models associated through both a hasMany and a belongsTo association.

    Consider this example:

    Child hasAndBelongsToMany Club
    

    Another way to look at this is adding a Membership model

    Child hasMany Membership
    Membership belongsTo Child, 
    ClubClub hasMany Membership.
    

    These two examples are almost the exact same. They use the same amount and named fields in the database and the same amount of models. The important differences are that the “join” model is named differently and it’s behavior is more predictable.

    In your example, you need a way to add and remove without editing other users Video links, therefore standard habtm will not suit you very well. Create a model for this “join” similar to the Membership model described above. Further, if you added a weight field, you could use the ordered behavior (that I wrote) to order each set of videos per bin. Then you would use the following fields

    id, bin_id, video_id, weight
    

    And set up bin_id as the ‘foreign_key’ in the behavior configuartion. Good luck

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

Sidebar

Related Questions

Need some help with Activerecord Querying in a has_many :through association. Model: Job class
I am using the activerecord-postgres-hstore gem with rails 3.2. I have setup hstore and
ActiveRecord's has_many and belongs_to methods both take a :foreign_key option. If I need to
In ActiveRecord model after_save callback I need to ROLLBACK transaction and return false. def
I have a simple ActiveRecord model called Student with 100 records in the table.
I have the following ActiveRecord query. It ran fine when the number of messages
I have an Rails ActiveRecord group query: a = Product.select(date(date) as date, count(id) as
In ActiveRecord how can we update a record without worrying/knowing primary key. If I
Castle ActiveRecord has BeforeLoad event but I need AfterLoad event too. I know it's
I have an ActiveRecord object, Corporation, and the only call in my project to

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.