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
3
votes
1 answer

Sequelize BelongsToMany self reference inverse SQL QUERY

Hello everyone hope you day is being great. I have searched all through the internet and here is my last line of hope. Hopefully some beautiful soul will explain to me why is that happening because I could not grasp from the documentations or other…
iwaduarte
  • 1,600
  • 17
  • 23
3
votes
0 answers

Sequelize - How to get sources id array on target belongsToMany association

Say I have a Student model and a Class model, and Student.belongsToMany(Class, through: {StudentClasses}). In the DB, StudenClasses table has id, student_id, class_id, and id like to have all the associated student_ids of a Class instance by that…
3
votes
1 answer

CakePHP 3.4 how to insert on a belongs to and has many relationship

On cakePHP 3.4, i have 3 tables with a belongs to and has many relationship: Ingredients, Products and IngredientsProducts: class IngredientsTable extends Table { public function initialize(array $config) { // Use through option…
3
votes
1 answer

Sequelize belongstomany associations : create a post and associate its existing tags

I got two models associates by belongsToMany associations. Posts.js : 'use strict'; module.exports = function(sequelize, DataTypes) { var Posts = sequelize.define('Posts', { title: DataTypes.STRING, body: DataTypes.STRING }, { …
3
votes
1 answer

Rails RESTful delete in nested resources

Okay, so here's an example scenario. There is a student resource resources :students, and students has and belongs to many collections: resources :clubs, resources :majors, etc. So we can set up our routes easily enough... resources :clubs do …
3
votes
1 answer

Deleting HABTM Association Record

I have two models, Posts and Tags. Posts HasAndBelongsToMany Tags. Let's say a Post 1 has the tags world, news, and paper. Now in the joining table, I want to remove the association between the Tag "paper" and Post 1, but the Tag "paper" should not…
Harsha M V
  • 54,075
  • 125
  • 354
  • 529
3
votes
1 answer

When to use belongsToMany vs hasMany in Laravel 5

There has been many similar questions but I found the accepted answers unsatisfactory. There is no clear answer as far as I could find in existing questions. For belongsTo() vs hasOne(), the common answer seem to be 'user' and 'phone' relationship.…
Evren Yurtesen
  • 2,267
  • 1
  • 22
  • 40
3
votes
1 answer

laravel 5.2 one pivot with many relations (belongstomany/polymorphic)?

he guys. i need your help. i have three questions about logic and best practice in laravel/eloquent/relation. first scenario i have four models/tables. 1.) model: Ticket | db: tickets 2.) model: Status | db: status 3.) model: Comment | db:…
3
votes
1 answer

Find record, that has ALL associated records

Say, we have a "Person" and "Favorite" models. "Favorite" is what this person likes: "music", "video", "sport", "internet", "traveling" etc. "Person" HABTM "Favorites", and "Favorite" HABTM "Persons" I need to find a Person, that has ALL listed…
AntonAL
  • 16,692
  • 21
  • 80
  • 114
3
votes
2 answers

How can I update hasandbelongstomany relations for multiple models at once in strongloop loopback

I have 2 models in a Strongloop Loopback API Products Tags Between those 2 models I have defined a hasAndBelongsToMany-relation. In my CMS I want a bulk-update functionality for my products, in which I can select many Products, and assign many…
3
votes
1 answer

Saving Data form in CakePHP

I had a problem with saving my data "my form does not passed a correct array", I want to know what is the error in my code (After reading the cookbook)I understand the array that passed to saving method should be like this array( (int) 0 =>…
3
votes
1 answer

Rails Admin errors out when where condition assoicated with a has_and_belongs_to_many association

I have a has_and_belongs_to_many association between my Activity model and Photo Model. class Activity < ActiveRecord::Base has_and_belongs_to_many :photos, -> { where(photos: { deleted: false })} end class Photo < ActiveRecord::Base …
3
votes
1 answer

HABTM relationship with a text/hidden field for ids

I'm creating a has_and_belongs_to_many relationship with Rails. Each group has many participants and each participant can be part of many groups. The relationship seems to be set up ok as I can use check boxes to add relationships using this in my…
3
votes
1 answer

Rails Single Table Inheritance with HABTM Fixture in unit testing returning NoMethodError: undefined method `singularize'

Imagine a model structure as follows: models/cross_sell_promotion.rb class CrossSellPromotion < Promotion has_and_belongs_to_many :afflicted_products, :join_table => :promotion_afflicted_products, :foreign_key => 'promotion_id',…
3
votes
2 answers

Laravel 4.1 - Eloquent lazy loading and filtering results based on pivot table value

I have 2 models, Sound and Tag: belongsToMany('Tag'); } }