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
129
votes
4 answers

Rails has_and_belongs_to_many migration

I have two models restaurant and user that I want to perform a has_and_belongs_to_many relationship. I have already gone into the model files and added the has_and_belongs_to_many :restaurants and has_and_belongs_to_many :users I assume at this…
126
votes
7 answers

Rails migration for has_and_belongs_to_many join table

How do I do a script/generate migration to create a join table for a has_and_belongs_to_many relationship? The application runs on Rails 2.3.2, but I also have Rails 3.0.3 installed.
ma11hew28
  • 121,420
  • 116
  • 450
  • 651
125
votes
11 answers

How to create has_and_belongs_to_many associations in Factory girl

Given the following class User < ActiveRecord::Base has_and_belongs_to_many :companies end class Company < ActiveRecord::Base has_and_belongs_to_many :users end how do you define factories for companies and users including the bidirectional…
opsb
  • 29,325
  • 19
  • 89
  • 99
52
votes
1 answer

Rails HABTM - Properly removing an association

I am developing a feature for creating specials, for a shopping website. One product can have more than one special, and obviously a special can have more than one product.. I am using a has_and_belongs_to_many relationship, so i have…
Marco Prins
  • 7,189
  • 11
  • 41
  • 76
41
votes
2 answers

How do I remove a single HABTM associated item without deleting the item itself?

How do you remove a HABTM associated item without deleting the item itself? For example, say I have 3 Students that are in a Science class together. How do I remove the Science objects from the StudentsClasses table without deleting the actual…
humble_coder
  • 2,777
  • 7
  • 34
  • 46
40
votes
2 answers

HABTM Relationship -- How Can I Find A Record Based On An Attribute Of The Associated Model

I have setup this HABTM relationship in the past and it has worked before....Now it isn't and I'm at my wits end trying to figure out what's wrong. I've looked through the rails guides all day and can't seem to figure out what I'm doing wrong, so…
ChrisWesAllen
  • 4,935
  • 18
  • 58
  • 83
30
votes
5 answers

Add record to a has_and_belongs_to_many relationship

I have two models, users and promotions. The idea is that a promotion can have many users, and a user can have many promotions. class User < ActiveRecord::Base has_and_belongs_to_many :promotions end class Promotion < ActiveRecord::Base …
27
votes
3 answers

habtm relationship does not support :dependent option

Is it true that HABTM relationships do not support the :dependent option? class Person < ActiveRecord::Base has_and_belongs_to_many :posts, :dependent => :destroy end I am trying rails edge.
Nick Vanderbilt
  • 36,724
  • 29
  • 83
  • 106
24
votes
2 answers

Multiple select issue with a HABTM relationship using Rails 4

Although the code seems to be right, when I try to send the form, the values of the multiple select aren't being sent. If I just remove the multiple option, everything works as expected considering just one value, but it's important to store more…
Diego Couto
  • 585
  • 1
  • 5
  • 16
22
votes
4 answers

has_and_belongs_to_many in Rails

Is there anything explicitly wrong with using has_and_belongs_to_many associations in rails instead of has_many :through? I'm aware of these articles describing differences and work arounds, but they are from 2006. From things I've read on SO, it…
Restore the Data Dumps
  • 38,967
  • 12
  • 96
  • 122
22
votes
1 answer

Rails habtm callbacks

Is there a way to add callbacks for when an item is added to a habtm relationship? For example, I have the following two models, User and Role: # user.rb class User; has_and_belongs_to_many :roles; end   # role.rb class Role;…
sethvargo
  • 26,739
  • 10
  • 86
  • 156
20
votes
3 answers

Rails: is it possible to add extra attribute to a has_and_belongs_to_many association?

What I mean is if I have two models, connected by a has_and_belongs_to_many association, can I store other data in the join table for each association? That is, the extra data would not be part of a single record in either table, but instead of the…
19
votes
3 answers

rails 3 habtm delete only association

class Company has_and_belongs_to_many :users end class User has_and_belongs_to_many :companies end when i delete a company, what's the best (recommended) way to delete ONLY the associations of the users from that company? (i mean not the…
Andrei S
  • 6,486
  • 5
  • 37
  • 54
17
votes
4 answers

HABTM - uniqueness constraint

I have two models with a HABTM relationship - User and Role. user - has_and_belongs_to_many :roles role - belongs_to :user I want to add a uniqueness constraint in the join (users_roles table) that says the user_id and role_id must be unique. In…
keruilin
  • 16,782
  • 34
  • 108
  • 175
17
votes
1 answer

Laravel belongsToMany where doesn't have one of

I have two tables: categories and videos, I then have a pivot table for these as it's a belongsToMany relationship. What I'm trying to do is get all of the videos where there isn't a single instance of the video being in one of many…
Karl
  • 5,435
  • 11
  • 44
  • 70
1
2 3
91 92