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

Rails active record, how can I query in HABTM relation

I have 2 Model which have HABTM relation User has_and_belongs_to_many :rooms Room has_and_belongs_to_many :users I also create the migration to join the table like this create_join_table :users, :rooms do |t| t.index [:user_id, :room_id] …
4
votes
2 answers

Determine if children added/deleted in Rails Callback

Scenario: I have a habtm relationship and would like to determine if children in one direction have been added or deleted. I'm trying to use callbacks but find i don't have a record of changes to the children. Is there something like…
Becky
  • 207
  • 2
  • 12
4
votes
2 answers

Rails: How to setup a form with multiple select tags with a HABTM relationship

I have bands, genres and bands_genres database tables with a HABTM relationship I have a form for creating new bands and I want users to be able to select 3 genres from 3 different dropdown select menus. How would I set up my form (and my create…
Shpigford
  • 24,748
  • 58
  • 163
  • 252
4
votes
1 answer

How to make a has_and_belongs_to_many association when creating an instance of a model

c = Course.create name:"Math1", ?? I'm trying to figure out how to associate this course to the existing students with has_and_belongs_to_many. Below is my schema with the associations. I'm having trouble creating instances of each and putting in…
4
votes
1 answer

Rails HABTM after_add callback fires before saving primary object

I have two ActiveRecord models having a HABTM relationship with eachother. When I add an AccessUnit through a form that allows zones to be added by checking checkboxes I get an exception that the AccessUnitUpdaterJob can't be enqueued because the…
4
votes
2 answers

Why use Nested types while creating part-of relationship (composition)

what is the benefit of using Nested type (instead of using variable/field-data of that class type) while creating part-of relationship (example engine is a part of Car) In other words what is the difference between following: (1) public class…
Tushar
  • 49
  • 3
4
votes
2 answers

Rails - deleting a single record in a intersection table habtm

I have a habtm relationship (assignments < assignments_candidates > candidates) I want to be able to delete one candidate off an assignment. here is my code so far @assignment = Assignment.find(:first, :joins => :candidates, :select =>…
Alex
  • 6,205
  • 7
  • 43
  • 53
4
votes
3 answers

nested form & habtm

I am trying to save to a join table in a habtm relationship, but I am having problems. From my view, I pass in a group id with: <%= link_to "Create New User", new_user_url(:group => 1) %>   # User model (user.rb) class User < ActiveRecord::Base …
brewster
  • 4,342
  • 6
  • 45
  • 67
4
votes
3 answers

Laravel Eloquent: Get record that contains two specific relations

I have a project where a user can create conversations with other users. A conversation can belongsToMany users and user can belongsToMany conversations. I now need to get the conversation in which two specific users participate. I tried a…
4
votes
2 answers

How do I do multiple has_and_belongs_to_many associations between the same two classes?

I have the following setup: class Publication < ActiveRecord::Base has_and_belongs_to_many :authors, :class_name=>'Person', :join_table => 'authors_publications' has_and_belongs_to_many :editors, :class_name=>'Person', :join_table =>…
Hermine D.
  • 1,119
  • 3
  • 14
  • 19
4
votes
2 answers

CakePHP 3 belongsToMany Validation

I am struggling with how to do validation with belongsToMany relationships. Namely, the classic recipes/ingredients relationship. I would like a recipe to always have an ingredient on create or edit. What would my validation look like in my…
Kris
  • 657
  • 1
  • 7
  • 19
4
votes
1 answer

CakePHP hasAndBelongsToMany (HABTM) Delete Joining Record

I have a HABTM relationship between Users and Locations. Both Models have the appropriate $hasAndBelongsToMany variable set. When I managing User Locations, I want to delete the association between the User and Location, but not the Location.…
Jason McCreary
  • 71,546
  • 23
  • 135
  • 174
4
votes
4 answers

HABTM association with Strong Parameters is not saving user in Rails 4

User model: has_and_belongs_to_many :events Event model: has_and_belongs_to_many :users Users controller: params.require(:user).permit(:role, {:event_ids => []}) Events controller: params.require(:event).permit(:subject, :location, :date, :time,…
4
votes
5 answers

How can I find all records for a model without doing a long list of "OR" conditions?

I'm having trouble composing a CakePHP find() which returns the records I'm looking for. My associations go like this: User ->(has many)-> Friends , User ->(has many)-> Posts I'm trying to display a list of all a user's friends recent posts, in…
gomezuk
  • 299
  • 6
  • 15
4
votes
2 answers

Rails has_and_belongs_to_many collection methods not showing up on object

I have a problem with has_and_belongs_to_many in a Rails 4 app. The setup is as follows: A User can have several Roles A Role can have several Permissions Since many Users can share the same Roles, and many Roles can share the same Permissions,…
arnemart
  • 14,180
  • 3
  • 17
  • 11