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

Strange error with Rails HABTM relationship

I am having strange error with following case of HABTM relationship class Deal < ActiveRecord::Base has_and_belongs_to_many :outlets end class Outlet < ActiveRecord::Base has_and_belongs_to_many :deals end produced very strange error when I try…
0
votes
1 answer

relating third table to has_and_belongs_to_many in ruby on rails

I am building a multiple choice survey with a set of fixed questions and fixed answers. A question has_many answers and an answer belongs_to question. The questions and answers will be seed data in the db and will be written in the seeds.rb file. …
0
votes
1 answer

rails 4 HABTM relation and extra fields on join table

What I have (pseudo code): model Document column :title HABTM :users model User column :name HABTM :documents Document has users (being approvers for document, either approve or not), and in this context join table should have…
0
votes
1 answer

Correct model associations in cakephp

I use HABTM to populate database. I have 3 models. User, Book, Comment. To join records in the tables books and users I use in HABTM users_books table, and to bind comments I use 2 primary keys, user_id and book_id, which both are set as foreign…
pt0
  • 175
  • 1
  • 14
0
votes
1 answer

Cakephp: Find entries starting with id xx

I want to find all entries in my database beginning with id X. The problem is, that i need to find them not just: id>=X but with a specific sortation (like "votes" or "created"). like that: $this->find('all',array('conditions' =>…
tobysas
  • 308
  • 5
  • 18
0
votes
2 answers

How to save to 2 HABTM and 1 belongsTo association with cakephp

I am trying to save data from one of my controllers named "EvidenceController.php". The problem I am having is that not all the associations with my Remark Model will save. Only Remark's association with Evidence will save, and it only saves the…
user908759
  • 1,355
  • 8
  • 26
  • 48
0
votes
1 answer

Simulating a belongs_to_many in rails

I have a situation where in theory I would need to use a belongs_to_many relationship. I have an Example model and a Sentence model. Each example object has one sentence but these sentences are not necessarily unique. So, for example, I could…
DavidP6
  • 307
  • 8
  • 19
0
votes
1 answer

Can a has and belongs to many association not call a query on the associated object

I have two models Activities and Users class Activity < ActiveRecord::Base has_and_belongs_to_many :users end class User < ActiveRecord::Base has_and_belongs_to_many :activities end I can load all the activities which are associated with users…
DanMatlin
  • 1,212
  • 7
  • 19
  • 37
0
votes
1 answer

hasMany vs. "hasMany through" DB structure for threaded message app

I'm making a PHP / MySQL web app with basic messaging functionality. Two users will have the ability to have a "conversation" which will contain a series of messages. Here are the model relationships that I conceived: User > many-to-many >…
emersonthis
  • 32,822
  • 59
  • 210
  • 375
0
votes
1 answer

HABTM checking for match of latest 3

Here's an interesting one for you folks... I have a HABTM (has_and_belongs_to_many) relationship between "Dogs" and "Trips". My goal is to find two result sets: 1) Dogs that have been on at least 1 of the last 3 trips and call that @dogs_current 2)…
Cameron
  • 1,000
  • 8
  • 11
0
votes
1 answer

CakePHP 2.x HABTM find data which have relationship by condition

I have a HABTM association between the Models Sdirectory and User like this: class Sdirectory extends AppModel { public $actsAs = array('Containable'); public $hasAndBelongsToMany = array( 'User' => array( …
Zeussi
  • 571
  • 1
  • 9
  • 22
0
votes
1 answer

ArgumentError: undefined class/module HABTM_Pubs in Rails 4

I'm making a system to generate pub quizzes for pubs. To ensure that a pub doesn't receive the same quiz twice, pubs and quizzes have a many to many association. class Quiz < ActiveRecord::Base has_and_belongs_to_many :pubs serialize :rounds,…
Kappie001
  • 898
  • 2
  • 10
  • 20
0
votes
1 answer

Rails display HABTM works, edit HABTM shows multiple copies of last entry

Situation I am trying to tie multiple phone numbers to a client with has_and_belongs_to_many. I am able to add a client and phone number. If I pull up a client where I have three phone numbers, it displays each number, but when I click on edit, it…
0
votes
1 answer

CakePHP HABTM condition

I want to make a condition on my HABTM attributes I have the following HABTM relations in CakePHP 2.x: Practise.php public $hasAndBelongsToMany = array( 'Attribute' => array( 'className' => 'Attribute', 'joinTable' =>…
lockdoc
  • 1,539
  • 1
  • 18
  • 31
0
votes
1 answer

Filter index view by related model attribute in rails

I have a rails 4 that has a Post model related to a Tag model by an habtm relation. Tags have a name field and a category field. Multiple tags can have the same category. I need a view that displays only posts that have at least one tag belonging to…
TopperH
  • 2,183
  • 18
  • 33