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

What is the fastest way to create mass HABTM associations in Rails?

I have two tables, with a HABTM relationship in Rails. Something like the following: class Foo < ActiveRecord::Base has_and_belongs_to_many :bars end class Bar < ActiveRecord::Base has_and_belongs_to_many :foos end Now I have a new Foo…
klochner
  • 8,077
  • 1
  • 33
  • 45
15
votes
3 answers

Rails: Has and belongs to many (HABTM) -- create association without creating other records

Spent all day on Google, but can't find an answer. :\ I have a HABTM relationship between Users and Core_Values. class CoreValue < ActiveRecord::Base has_and_belongs_to_many :users class User < ActiveRecord::Base has_and_belongs_to_many…
jmccartie
  • 4,956
  • 8
  • 50
  • 71
14
votes
3 answers

Efficient ActiveRecord has_and_belongs_to_many query

I have Page and Paragraph models with a has_and_belongs_to_many relation. Given a paragraph_id, I'd like to get all matching pages. e.g.: pages = Paragraph.find(paragraph_id).pages.all However, this takes two queries. It can be done in one…
Michiel de Mare
  • 41,982
  • 29
  • 103
  • 134
13
votes
3 answers

Rails habtm and finding record with no association

I have 2 models: class User < ActiveRecord::Base has_and_belongs_to_many :groups end class Group < ActiveRecord::Base has_and_belongs_to_many :users end I want to make a scope (that's important - for efficiency and for ability to chain…
schiza
  • 1,900
  • 1
  • 15
  • 18
12
votes
3 answers

Create join table with no primary key

I have two tables with a many to many relationship that I am using has_and_belongs_to_many to define the association. class Foo < ActiveRecord::Base ... has_and_belongs_to_many :bar ... end class Bar < ActiveRecord::Base ... …
Keith
  • 121
  • 1
  • 1
  • 3
12
votes
1 answer

Trying to use accepts_nested_attributes_for and has_and_belongs_to_many but the join table is not being populated

I am learning RoR and trying to use accepts_nested_attributes_for and has_and_belongs_to_many to submit information that would traditionally be two forms. I have read on some sites they are compatible, some sites they aren't compatible, and some…
davidstites
  • 687
  • 1
  • 6
  • 18
11
votes
2 answers

Records in join table destroyed automatically in HABTM association?

Let's say I have an association where User has and belongs to many Roles. When I destroy the user, is the record in the join table automatically removed as well? Or do I need to use :dependent => :destroy? What about if I destroy a Role? class…
keruilin
  • 16,782
  • 34
  • 108
  • 175
11
votes
4 answers

acts_as_list with has_and_belongs_to_many relationship

I've found an old plugin called acts_as_habtm_list - but it's for Rails 1.0.0. Is this functionality built in acts_as_list now? I can't seem to find any information on it. Basically, I have an artists_events table - no model. The relationship is…
11
votes
6 answers

Yii framework Many to Many relationships

What is the method to save and update Many to Many relationship in Yii framework?
Chris
  • 8,168
  • 8
  • 36
  • 51
11
votes
3 answers

CakePHP how to retrieve HABTM data with conditions?

I use CakePHP 2.2.2 I have 3 tables: restaurants, kitchens and kitchens_restaurants - join table for HABTM. In Restaurant model I have: public $hasAndBelongsToMany = array( 'Kitchen' => array( 'className' =>…
user1327
  • 938
  • 3
  • 10
  • 28
10
votes
2 answers

Ordering of has_and_belongs_to_many associations

In my rails app I have two models that are related by has_and_belongs_to_many. This means there is a join table. Imagine the scenario where I am adding users to a game. If I want to add a user, I do: @game.users << @user Supposing that I want to…
cmaughan
  • 2,596
  • 5
  • 30
  • 35
10
votes
2 answers

Rails habtm joins

I have this relationship between categories, products & brands: class Brand < ActiveRecord::Base has_many :products end class Category < ActiveRecord::Base has_and_belongs_to_many :products end class Product < ActiveRecord::Base …
Nick
  • 169
  • 1
  • 12
10
votes
1 answer

How to have many-to-many relationship in rails

I am new to rails, and am trying to set up a many-to-many relationship in my rails project. I have a small strategy, but I am not sure if its the correct way. Aim: I have a table of users, and a table of groups. Users can be part of many groups, and…
Karan
  • 14,824
  • 24
  • 91
  • 157
9
votes
4 answers

BelongsToMany relation. How to get unique rows

I have next 'member_companies' table in DB: And in model Member has a relation : public function companies() { return $this->belongsToMany(Company::class, 'member_companies'); } And it return me all companies with dublicates. For example,…
Igor Ostapiuk
  • 589
  • 3
  • 7
  • 23
9
votes
2 answers

Rails 3.1 Ransack HABTM

Is HABTM supported by Ransack? Having the models: Shop HABTM Categories Category HABTM Shops Can I use ransack to search a Shop by a single category? What does the form look like?
1
2
3
91 92