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

CakePHP retrieving HABTM data

Quick setup: User hasAndBelongsToMany Course courses_users __________________________ id | user_id | course_id | 1 | 1 | 1 | 2 | 1 | 3 | 3 | 2 | 5 | I need to load all the Courses for the User with…
Christopher Francisco
  • 15,672
  • 28
  • 94
  • 206
0
votes
1 answer

CakePHP HABTM Association not working

I've been trying to find an answer to my problem for hours. I am currently working with cakePHP 2.4. I have two models, Users and Groups. I have created the following associations for each: (User.php) public $hasAndBelongsToMany = array( 'Group'…
0
votes
1 answer

Ruby on Rails HABTM collection_select Database Call

Alright, so I've been working on this for a while and can't seem to figure it out. I've read extensively on guides.rubyonrails.org and thats how I got started. I successfully created the following HTML code. <%=…
0
votes
2 answers

CakePhp $hasAndBelongsToMany not saving multiple select items as expected

I have the following code setup (snipped for brevity) class BasePackage extends AppModel { public $name = 'BasePackage'; public $hasAndBelongsToMany = array('ProductSubtype', 'ProductType'); } class ProductType extends AppModel { public…
0
votes
1 answer

Build Rails Object From 2 has_and_belongs_to_many Associatons

Currently I've defined two models, Accounts and Users, within my app using has_and_belongs_to_many associations. An account within the app refers to an organization such as a company. I'm able to get the current user easily, but I'm struggling to…
Mikael Kessler
  • 1,235
  • 1
  • 15
  • 27
0
votes
1 answer

CakePHP - One HABTM With Two Foreign Keys

I've been trying to save two foreign keys to the same row of a join table with no success. My Client model has two HABTM association with both User and Group models, a user can have many clients and create clients under many different groups. The…
dcd0181
  • 1,493
  • 4
  • 29
  • 52
0
votes
1 answer

Creating a clean HABTM query

i've been working to try and getting a clean HABTM query for MySQL i have the following tables auth_users id: 1,name: test auth_groups_users user_id: 1,group_id: 1 user_id: 1,group_id: 2 auth_groups id: 1,name: default_group id: 2,name:…
Tobias Hagenbeek
  • 1,212
  • 3
  • 15
  • 30
0
votes
1 answer

Rails trying to send the id for a habtm model join table?

So I have two rails models - user and role - each specified with a habtm and a join table of users_roles. When I save from my user form, which has checkboxes for which roles to choose, rails is trying to manually insert a value into the id column of…
evanmcd
  • 1,967
  • 3
  • 32
  • 52
0
votes
1 answer

Rich many-to-many assosiation. Only allow one record

I need to limit my rich many-to-many association table to only one association per unique par. (rails). That means i want terminals (model 1) and subscriptions (model 2) to have one custom_price (rich join) not any more than that. How is this done…
0
votes
1 answer

Mongoid: finding documents that are referencing another one (referenced 1-N)

I have a MongoDB database that uses mongoid referencing according to the documentation. Here are my models: class Mydoc include Mongoid::Document # ... has_and_belongs_to_many :editors, class_name: 'User', inverse_of: nil # ... end…
0
votes
1 answer

Counting Current HABTM Relationships. CakePHP

I need to assign hospitals to a group. I've decided to do this via habtm. class FavoriteHospital extends AppModel { public $actsAs = array('Containable'); ... var $hasAndBelongsToMany = array( 'Hospital' => array( …
Christos Hrousis
  • 707
  • 4
  • 16
0
votes
1 answer

Rails 3 multiple has_and_belongs_to_many relationships

I'm creating a rails app that contains lists. Lists are made up of: List_Source (e.g. National Geographic) List_Elements (e.g. Tanzania) List_Tags (e.g. Travel) A list has a single source, multiple elements, and multiple tags. A source has…
0
votes
2 answers

Moving a many to many to a one to one in rails

I have a relationship between two models that is currently a many to many. I'd like to move it to a one to one relationship, because that's what all of the data has turned out to be. Is there a seamless way to do this? Via a migration and model…
0
votes
1 answer

Setting up self-referencing HABTM relationship in Rails 4

I'm looking to set up a self-referencing has_and_belongs_to_many relationship using rails 4, with a postgres db. Basically I've got a Single Inheritance Table set up called Resource, which holds People, Places, and Things in it. (This works…
0
votes
1 answer

Ruby on Rails HABTM adding records to table

I am trying to use a Has and belongs to many relationship to link users to teams, the issue I am having is that I cannot work out how to add the ids into my user_teams table. In my model I have done the following User Model has_and_belongs_to_many…
Boss Nass
  • 3,384
  • 9
  • 48
  • 90