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

Find records that are not associated with other model in HABTM

I have two models 'Article' and 'List' both associated with HABTM association. I also have 'articles_lists' join table that has 'article_id' and 'list_id'. Now I would like to display all the articles that are not associated with any lists. Which is…
0
votes
1 answer

A related Model isn't being validated

I currently have the following models: class Category extends AppModel { var $name = 'Category'; /*var $validate = array( 'name' => 'multiple' ); no idea how to use this */ var $hasAndBelongsToMany = array( …
Teej
  • 12,764
  • 9
  • 72
  • 93
0
votes
2 answers

How to edit additional data in HABTM tables?

How do I update additional data in HABTM tables. For Example: I have movies, people and HABTM movies_people tables, but there is additional persontype_id field in movies_people table which indicates role of this person in that particular movie. How…
totocaster
  • 6,243
  • 5
  • 37
  • 46
0
votes
1 answer

CakePHP HABTM association rules

If I want to create a category and be able to link products to it by tags I can like so: Create the category and product tables. Create a tags table with tags like: Ruby, Earrings, White-Gold Create a category_tags and product_tags table to map…
JD Isaacks
  • 56,088
  • 93
  • 276
  • 422
0
votes
1 answer

Laravel update multiple of the same belongsToMany relationship

I have an Orders table and a Products table, my relationship between these is setup as a belongsToMany relationship, which works fine. However, an Order can have the same Product multiple times (in case they want to order more and the admin can…
Karl
  • 5,435
  • 11
  • 44
  • 70
0
votes
1 answer

Incorrect behavior of delete() method

I have three models: Project, User, ProjectsUser. The ProjectsUser table is used for HABTM relations between Project and User. This is remove() method of Project model: function remove($project_id, $user_id) { /* *…
Clark
  • 2,083
  • 6
  • 32
  • 47
0
votes
1 answer

HABTM relationship checkboxes with an option to add more

I have a Company and a Product model with has_and_belongs_to_many relationship. # app/models/company.rb class Company < ApplicationRecord has_and_belongs_to_many :products end # app/models/product.rb class Product < ApplicationRecord …
Ammar Shah
  • 131
  • 2
  • 10
0
votes
1 answer

Ruby on Rails: find doesn't retrieve all objects associated when using conditions

I have 2 models with a many-to-many relationship (simplified here with books-authors example): class Book < ActiveRecord::Base has_and_belongs_to_many :authors end class Author < ActiveRecord::Base has_and_belongs_to_many :books end I…
0
votes
1 answer

BelongsToMany of Laravel 5.4 does not work

I am creating a table where it displays a list of requests, I have a relation between the ID's 'erp_createdid' and 'erp_productid'. To display the name of the products, I created a belongsToMany, in which I specify my two 'ID's'. Order model class…
Vinícius
  • 443
  • 1
  • 8
  • 29
0
votes
1 answer

Sequelize has and belongs to many

Is there a way to do a polymorphic self-association with a through table (e.g. Collection has and belongs to many Collections)? Trying to adapt http://docs.sequelizejs.com/manual/tutorial/associations.html#n-m to this scenario: // Inside…
0
votes
1 answer

OrderBy In A HasAndBelongsToMany Relation

Hi can I get help from an expert in ActiveRecord? I am looking for a solution for a HasAndBelongsToMany relation. I currently have the following [HasAndBelongsToMany(Table = "SupplierRecordUrls",ColumnKey = "SupplierID", ColumnRef =…
0
votes
1 answer

CakePHP 2.9 converting CSV string to HABTM data

I am trying to convert a large database (~3m rows) that contains the following data set titled "Posts": +-------|---------------|-----------------------+ | id | name | tags | …
Tricky
  • 410
  • 8
  • 17
0
votes
1 answer

Rails HBTM join_table overwirting table_name

I'm running Rails 2.3.2 and doing: class StandardWidget < ActiveRecord::Base has_and_belongs_to_many :parts, :join_table => "widgets_parts", :association_foreign_key => "widget_custom_id" end class Part < ActiveRecord::Base …
ma11hew28
  • 121,420
  • 116
  • 450
  • 651
0
votes
1 answer

Rails has_and_belongs_to_many association with other query

Im using Rails 5 and I wanna doing has_and_belongs_to_many relation with other query. Its instead of merge two queries but its not same. Actually I wanna do this clean…
0
votes
1 answer

Ruby on Rails update method is accidentally called.. why?

I'm still new to Ruby on Rails and I am stucking at one Point. My Database contains a Teacher and a Course model. They are associated with a has_and_belongs_to_many Association (at the moment it shouldn't be changed). Course Detail View:

homior
  • 161
  • 1
  • 12