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

Add HABTM data in controller

In my app, Users HABTM Solicitations. After saving the form using $this->Solicitation->save($this->request->data), I need to add another user_id value in the solicitations_users table. $data[] = array('User' => array('id' => 5), 'Solicitation' =>…
Igor Martins
  • 2,015
  • 7
  • 36
  • 57
4
votes
2 answers

RSpec testing relationship logic

I'm testing the following: Account class Account < ActiveRecord::Base has_many :ownerships has_many :brands, :through => :ownerships end Ownership join model class Ownership < ActiveRecord::Base belongs_to :brand belongs_to…
Antony Sastre
  • 617
  • 8
  • 19
4
votes
1 answer

Unpermitted Parameters with Strong Params with many-to-many relationship

I have a has and belongs to many relationship setup like so: has_and_belongs_to_many :players, :class_name => "User" # In app/models/team.rb has_and_belongs_to_many :teams # In app/models/user.rb I'm using a form to create…
Tom Prats
  • 7,364
  • 9
  • 47
  • 77
4
votes
2 answers

Rails 4 many to many association not working

Ruby on rails newbie here. Trying to create a starter blog app and having trouble with many to many association between my models. I have 2 models - Post, Category that have a many to many association between each other. My problem: When I create a…
4
votes
1 answer

Not saving the HABTM Association when creating a record

I have those two Models, in a HABTM Relationship: The Project is using Rails 4, so no attr_accessible tags wine.rb class Wine < ActiveRecord::Base has_and_belongs_to_many :pairings, class_name: 'Food', join_table: 'foods_wines',…
Martin Lang
  • 831
  • 11
  • 20
4
votes
1 answer

Rails HABTM with polymorphic relationships

I have a table Category which can have many Businesses and Posts. And a Business/Post can have many Categories so I created a polymorphic tabled called CategoryRelationship to break up the many to many relationship. Business model has these…
bcackerman
  • 1,486
  • 2
  • 22
  • 36
4
votes
6 answers

Filtering model with HABTM relationship

I have 2 models - Restaurant and Feature. They are connected via has_and_belongs_to_many relationship. The gist of it is that you have restaurants with many features like delivery, pizza, sandwiches, salad bar, vegetarian option,… So now when the…
Miha Rekar
  • 1,237
  • 12
  • 15
4
votes
1 answer

CakePHP alias breaks HABTM Model

Consider the following HABTM relation in CakePHP 2.2.3: class User extends AppModel { public $hasAndBelongsToMany = array( 'Role' => array( 'className' => 'Role', 'joinTable' => 'roles_users', …
Jay
  • 2,141
  • 6
  • 26
  • 37
4
votes
3 answers

Rails - View not updating with new data until I clear the cache

I have a database that contains users and groups with a has_and_belongs_to_many relationship. When a new group is added, it gets created but the user's membership to the group doesn't seem to propagate until I clear the cache or login with an…
Kim
  • 161
  • 7
4
votes
1 answer

Confused about Rails has_and_belongs_to_many Association

I have two models, items and categories which have a many-to-many relationship using the has_and_belongs_to_many association. In my models I have class Item < ActiveRecord::Base has_and_belongs_to_many :categories end and class Category <…
steffi2392
  • 1,345
  • 3
  • 19
  • 19
4
votes
1 answer

Rails: Creating a record with HABTM relationship

I've been doing this to create a record with a HABTM relationship: @project = Project.new(:title => params[:project][:title], :percent_complete => params[:project][:percent_complete]) @project.users <<…
Zhao Li
  • 4,936
  • 8
  • 33
  • 51
3
votes
1 answer

How to code tag cloud in Ltihium without HABTM?

I'm a little foggy with Lithiums relationships. I'm trying to create a tag cloud using Lithium but I'm not sure how to do this without using HABTM relationships. I'm using MySQL, btw. Any suggestions? :edited to add sample code: Here is a very…
Housni
  • 963
  • 1
  • 10
  • 23
3
votes
1 answer

How can i setup load_and_authorize_resource through a habtm association?

I have tasks which I want to display in project context. They are associated through a habtm association. So I have three tables: projects, tasks and projects_tasks. resources :projects do resources :tasks end class Project < ActiveRecord::Base …
3
votes
1 answer

has_and_belongs_to_many or has_many for user/group relationship?

I'm working on a Rails 3.1 app that has the following models: User: class User < ActiveRecord::Base has_and_belongs_to_many :groups has_many :ownerships, :class_name => 'Group' end Group: class Group < ActiveRecord::Base …
Steve Davis
  • 959
  • 1
  • 11
  • 25
3
votes
1 answer

Using Formtastic for checkboxes for a habtm association in Rails3

I followed the directions in Railscast #17 HABTM Checkboxes (revised) to get this code for adding services to a project using a has_and_belongs_to_many association: <% Service.all.each do |service| %> <%= hidden_field_tag…