Questions tagged [has-and-belongs-to-many]

A has_many :through association set up a many-to-many connection with another model.

In Ruby on Rails, a has_many :through association is often used to set up a many-to-many connection with another model. This association indicates that the declaring model can be matched with zero or more instances of another model by proceeding through a third model.

1372 questions
0
votes
2 answers

CakePHP HABTM model problem

I have a problem when using CakePHP HABTM. I have the following models. class Repositorio extends AppModel{ var $name="Repositorio"; var $hasAndBelongsToMany = array( 'Sesion' => array( 'joinTable' =>…
Lobo
  • 4,001
  • 8
  • 37
  • 67
0
votes
0 answers

Activerecord Rails HABTM relation search by ids

I have has_and_belongs_to_many association between two models Chat and User. I need to search for a chats by chat_members (user_ids). for example: Chat.where(users: User.last(3)) Is there an easy way to implement this task?
0
votes
0 answers

Association not found error after upgrading to Rails 6

After upgrading my app to Rails 6, model which has many to many relationship causes an error saying that Association named 'kulits_efloresensi_kulits' was not found on Kulit; perhaps you misspelled it? On Rails 5 it was working fine. Here are the…
0
votes
1 answer

Rails arel query for matching multiple habtm records

I have a setup as follows, Post - has_and_belongs_to_many :tags Tag - has_and_belongs_to_many :posts THE PROBLEM: I want to find all the posts which have social, informative tags in it, I can also use their ID's suppose 1 and 2. Using arel or…
0
votes
1 answer

rails 4 belongs_to and has_many to the same model

I have an Opportunity model that belongs_to Section model. Section has_many opportunities. class Opportunity < ActiveRecord::Base belongs_to :section class Section < ActiveRecord::Base has_many :opportunities Opportunity model has to have…
0
votes
1 answer

Rails Query to join Child class with Parent class via a through class

I have the following class class Order has_many :order_issues, through: :order_deliveries, inverse_of: :order end class OrderDelivery has_many :order_issues, as: :issuable, inverse_of: :order_delivery belongs_to :order, inverse_of:…
0
votes
1 answer

filter Sequelize belongsToMany

I have the following problem: I defined my tables (product and collection) like this: module.exports = (sequelize, type) => { return sequelize.define('product', { id: { type: type.UUID, primaryKey: true, …
0
votes
1 answer

Option from collection_select creates a new one on submit - Rails 5

Today I've been working on a HABTM association between my Plugins and Categories. I got it almost working, but run into trouble with the collection_select. I have a select in my form and I succesfully call all the existing Categories, but when I…
0
votes
1 answer

Polymorphic vs HABTM relationships for a tagging system

I am currently making a database for a rails application. This schema involves three models: Photo, Object and Tag. Both photos and objects need to be "taggable". Tags are unique Tags can belong to a Photo, or an Object, or both Rails offers a…
0
votes
1 answer

Show items without record in jointable

I'm building an application with CakePHP on which you can fill in surveys. The problem i came up with is the following: I have two tables: Surveys / SurveysUsers (HABTM) When somebody fills in a survey a record will be made in SurveysUsers and when…
Dwayne
  • 1
0
votes
1 answer

Storing selected product options in a shopping cart in CakePHP

I am building a shopping cart in CakePHP and currently have the following relationships: Product HABTM (HasAndBelongsToMany) Options Cart HABTM Products Pretty standard? I'm just not sure what to do when a user selects some options for the product…
0
votes
2 answers

CakePHP - save multiple HABTM data w/ dropdowns in same form

When a user adds an Event, I want them to be able to choose which band(s) are playing at that event. I already have my Events table and my Bands table each with HABTM model associations to the other. On my "add event" page, I have a dropdown…
Dave
  • 28,833
  • 23
  • 113
  • 183
0
votes
1 answer

is this a really bad way to do queries across multiple HABTM models?

I am trying to query across two HABTM tables. I have Leases Managers Tenants Properties. Managers HABTM Tenants Managers HABTM Properties Tenants HABTM Leases What I want to do is find a list of Properties linked to Managers linked to Tenant. I…
user559540
0
votes
1 answer

Belongs_to too many columns ID

I'm trying to create a Matches table. This matches table will look to get it's information from a Teams table. I'm having trouble getting the association to work. class Match < ActiveRecord::Base # end class Team < ActiveRecord::Base …
0
votes
1 answer

Laravel belongsToMany.belongsTo nested relationship query where child column = parent id

I have two models (and database tables with Laravel naming conventions) and I am trying to Eager Load the Collections with Equipment and Equipment.collection_meters where collection_meter equals Collection.id. How can I access the collections.id in…