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

HABTM relationship with "dependent: :destroy"

I have 3 models in my test Rails 4 project. Company Project Contact A Company has_many Contacts and has_many Projects. A Project belongs_to a Company. A Contact belongs_to a Company. I want to be able to create a relationship between a Project and…
Eelco
  • 111
  • 1
  • 7
0
votes
0 answers

Why is updating a form duplicating my form fields and duplicating records with habtm and nested form?

I have a nested form inside a parent form that allows you to add fields inside the parent form. The idea is that a book can have many schools and the school belongs to the book(habtm) relationship. here are my…
0
votes
1 answer

How can I create a record on a relationship and if already exists ignore and save to association instead?

I have the following setup in rails: book.rb can have many schools with a has_and_belongs-to_many :schools class Book < ActiveRecord::Base before_save :get_school include PgSearch pg_search_scope :search, :against => [:title, :slug,…
0
votes
1 answer

Rails + simple role system through associative table

So I have the Ninja model which has many Hovercrafts through ninja_hovercrafts (which stores the ninja_id and the hovercraft_id). It is of my understanding that this kind of arrangement should be set in a way that the associative table stores only…
0
votes
2 answers

HABTM association

Im trying to create a HABTM association between my products and tags tables. I ran the automated migration in my terminal as: rails generate migration create_products_tags_join_table then rake db:migrate After I ran those commands I assumed that…
bigREDcode
  • 159
  • 1
  • 3
  • 11
0
votes
1 answer

CakePhp: Multiple Relationships with one Model

I have some trouble with two associations to one Model.. A little pic makes my problem hopefully a bit clearer.. http://imgur.com/hxL06u2 (sorry i cant post pictures here) so i have users who can write/own articles.. one user can write multiple…
tobysas
  • 308
  • 5
  • 18
0
votes
1 answer

HABTM accessing array value in condition

Hey my problem is to define a condition for a HABTM Model (int) 0 => array( 'Article' => array( 'id' => '', 'title' => '', 'body' => '', 'created' => null, 'modified' => null ), 'Channel' =>…
0
votes
2 answers

Ruby on Rails belongs_to many

Here is my situation : Items : has_many :games Users : has_many :games Games : belongs_to :user belongs_to :item On my Item page i have a link to create a new game. How to get the item ID in a secure way ? Because in my database I need to store…
ZazOufUmI
  • 3,212
  • 6
  • 37
  • 67
0
votes
1 answer

Delete single row from HABTM join table

I've got model that use ExtendAssociations, but for some reason deleteHABTM doesn't seem to work on some models (no idea :/). Is there an easy way to just delete a single row from the join table?
pawelmysior
  • 3,190
  • 3
  • 28
  • 37
0
votes
1 answer

Cakephp HABTM many to many relation

Hello I'm having trouble setting up a simple HABTM in cakephp. Please tell me what is wrong? I followed the documentation closely. My controller (it's a REST service) class UsersController extends AppController { public $components =…
Alejandro
  • 1,159
  • 3
  • 16
  • 30
0
votes
1 answer

RoR has_and_belongs_to_many

I have two database tables being domains and categories. Domains can have many categories and categories can have many domains. Both models are setup using has_and_belongs_to_many. Logically when a category is associated with a domain conversely a…
Dercni
  • 1,216
  • 3
  • 18
  • 38
0
votes
2 answers

Trying to save duplicate associations in CakePHP HABTM relationship on save

I would love to know the answer to this one as well - https://stackoverflow.com/questions/24447879/cakephp-habtm-i-want-duplicates-at-save I've tried a few different things in regards to unique = false, saveAll saveAssocaited, array formatting. No…
0
votes
2 answers

Rails Query based on hasManyBelongsToMany relation

I have the following models venues(id, name, ....) categories(id, name, ....) categories_venues(id, venue_id, category_id) I want to query venues based which have the relation of Categories ID = (1)/(2) or (1 and 2) How can i do a where query for…
Harsha M V
  • 54,075
  • 125
  • 354
  • 529
0
votes
1 answer

Laravel 4 - relationships

I have two tables: 'Articles' and 'Images'. In my Articles I have: `... 'body' 'image-1' 'image-2' 'image-3' 'image-4' ...` I want to relate every article with one, two, three,... images stored in my database. And, of course, all the images can…
JaviZu
  • 487
  • 1
  • 8
  • 20
0
votes
1 answer

Form for HABTM associations

I have two models, Album and Image. These have has_and_belongs_to associations with eachother. An album has many images, and an image can belong to many albums. Is it correct to specify HABTM then? After I create an album, it redirects to the album…