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

ActiveRecord::AssociationTypeMismatch for HABTM in rails

I have got two models Zipcode & Plan class Zipcode < ActiveRecord::Base self.primary_key = 'code' has_and_belongs_to_many :plans, :class_name => "Plan", :join_table => "plans_zipcodes", :foreign_key => "code" end class Plan <…
3
votes
1 answer

Rails 4 - best_in_place gem and many-to-many association

I am struggling to add the ability to update a contact languages using best_in_place (a jQuery inplace-editor gem). I have a contact object, a language object and a has_and_belongs_to_many :languages relationship between contacts and languages.…
3
votes
1 answer

Rails HABTM: Post.title uniqueness per User only

In Rails 4, I have a typical HABTM setup; class Author < ActiveRecord::Base has_and_belongs_to_many :posts end class Post < ActiveRecord::Base has_and_belongs_to_many :authors end I'm wanting to validate the uniqueness of Post.title, but only…
3
votes
2 answers

Accessing associated models of HABTM relationships via bindModel() w/o recursion

The Problem In A Nutshell I want to retrieve data from Model A that HABTM Model B via a find() operation in Model B's controller without relying on extensive recursion. $this->ModelB->bindModel('hasMany' =>…
Jonline
  • 1,677
  • 2
  • 22
  • 53
3
votes
2 answers

HABTM associantion: Unpermitted parameters

What's up folks! I've been trying to implement an association has_and_belongs_to_many in my rails (4) application. Here is my code: My Category model class Admin::Category include Mongoid::Document ... has_and_belongs_to_many :estimates,…
3
votes
1 answer

Saving HABTM data while also creating new record in related table with CakePHP

I'm building a site that works as a social network. I have a profiles table and a Profile model. Profile HABTM Interests Profile HABTM Hobbies etc... There are a couple of pre-defined records in the hobbies and interests tables, which users can…
Botch
  • 419
  • 5
  • 19
3
votes
2 answers

DataMapper has n through Resource DELETE (Remove from association) not working

I'm have this two classes, class User include DataMapper::Resource property :id, Serial property :name, String has n :posts, :through => Resource end class Post include DataMapper::Resource property :id, Serial property…
zanona
  • 12,345
  • 25
  • 86
  • 141
3
votes
1 answer

best way to validate ownership with HMABTM

I have three models like this: class User < ActiveRecord::Base has_many :items has_many :other_itmes end class Item < ActiveRecord::Base belongs_to :user has_and_belongs_to_many :other_items validate :validate_other_item_ownership def…
jigfox
  • 18,057
  • 3
  • 60
  • 73
3
votes
2 answers

Polymorphic relationships with pivot table in Laravel?

I got 3 models, Article, Building, Person. These models need to reference each other in a few ways. For example the building needs to be able to reference collections of Person like $building->architects(), $building->owners(), a article might…
3
votes
2 answers

cakephp habtm relationship (saving data)

Question related to HABTM has been posted in some good numbers on stackoverflow but I am still looking for a solution to my problem. I am creating an application that allows for creation of topics based on a particular subcategory. While adding a…
Gaurav Sharma
  • 2,830
  • 1
  • 37
  • 54
3
votes
1 answer

CakePHP find items without HABTM relationship record

Simply put: Tags HABTM Documents Is there a way to find all Tags that don't have a Document associated? I've started with this: $freeTags = $this->Tag->find('all', array( 'conditions' => array( ), 'contain' =>…
Elwhis
  • 1,241
  • 2
  • 23
  • 45
3
votes
2 answers

Trying to save HABTM model data in CakePHP 2.0

I have a data model that requires the use of HABTM, as follows: surveys ( id int(11) NOT NULL AUTO_INCREMENT, user_id int(11) NOT NULL, title varchar(50) DEFAULT NULL, created timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, …
3
votes
2 answers

Rails HABTM middle values

I have to following relationship: class Course < ActiveRecord::Base attr_accessible :name has_and_belongs_to_many :users end class User < ActiveRecord::Base attr_accessible :name has_and_belongs_to_many :courses end Then I have the…
fxe
  • 575
  • 1
  • 4
  • 15
3
votes
3 answers

CakePHP GROUP BY with WHERE and COUNT on HATBM table

Although I'm aware of the group, contain, fields and order options on a find(), I just cannot seem to make the following query with CakePHP 2.2.3 : SELECT `User`.*, SUM(`Influence`.`id`) AS matches FROM `db`.`users` AS `User` LEFT JOIN…
Nicolas
  • 2,754
  • 6
  • 26
  • 41
3
votes
2 answers

Rails 3 - join table not updated when creating new HABTM association

I've spent the last three days on this and I can't find any answer anywhere. I'm trying to learn Rails, and I started with what seemed a pretty easy application. I have 2 models: class Album < ActiveRecord::Base has_and_belongs_to_many :users …