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

HABTM, or multiple belongs_to?

I'm teaching myself Rails, and as a test project I'm mocking up a simple question/answer app similar to stackoverflow. In my simplified version I have: questions answers users (the authors of questions and answers) I get that answers belong to…
jefflunt
  • 33,527
  • 7
  • 88
  • 126
7
votes
1 answer

Ransack and acts-as-taggable-on issues

I`ve got a model with several taxonomies using acts-as-taggable-on class Film < ActiveRecord::Base attr_accessible :mood_list, :genre_list, :tag_list, :country_list acts_as_taggable_on :countries, :genres, :moods, :tags end I`m trying to make…
7
votes
3 answers

Has and belongs to many relationship with multiple databases

I have a situation where I have two models, companies and permissions, where companies is in a separate database from my permissions database. This is a has and belongs to many relationship because each company can have many permissions and each…
Steropes
  • 4,600
  • 2
  • 22
  • 26
7
votes
2 answers

Rails HABTM self join error

In my application a user can follow many users and can be followed by many users. I tried to model this using has_and_belongs_to_many association class User < ActiveRecord::Base has_and_belongs_to_many :followers, class_name: "User", foreign_key:…
Nitish Parkar
  • 2,838
  • 1
  • 19
  • 22
7
votes
1 answer

How can I load HABTM-with-foreign-key relationships in my fixtures?

I have the following two models: School and User, and a HABTM relationship between them, with a join table. In this join table, the foreign key refering to the User table is not called user_id but student_id. class School < ActiveRecord::Base …
Julien
  • 1,158
  • 9
  • 19
7
votes
1 answer

Rails, Ransack: How to search HABTM relationship for "all" matches instead of "any"

I'm wondering if anyone has experience using Ransack with HABTM relationships. My app has photos which have a habtm relationship with terms (terms are like tags). Here's a simplified explanation of what I'm experiencing: I have two photos: Photo 1…
Andrew
  • 42,517
  • 51
  • 181
  • 281
7
votes
4 answers

Self "HABTM" or "HasMany Through" concept confusion

Bounty: +500 rep bounty to a GOOD solution. I've seriously banged my head against this wall for 2 weeks now, and am ready for help. Tables/Models (simplified to show…
Dave
  • 28,833
  • 23
  • 113
  • 183
7
votes
2 answers

Rails - Seeding HABTM associations

Equipment.create(name: "Room to run") Equipment.create(name: "Pull-up bar") Workout.create( description: "Do 100 pull-ups then run 5km", :equipment => Equipment.where(:name => 'Pull-up bar')) Equipment and Workouts have a HABTM relationship.…
6
votes
4 answers

Ruby/Rails - Check If Child Id Exists Within Record of HABTM Relationship

I have a set of resources called Tasks and Posts and there are in a has_and_belongs_to_many (HABTM) relationship with each other. There is also a join table connecting their values. create_table 'posts_tasks', :id => false do |t| t.column…
ChrisWesAllen
  • 4,935
  • 18
  • 58
  • 83
6
votes
2 answers

How to set up a typical users HABTM roles relationship

I'm quite new to this and I'm using cancan + devise for my user auth. However I'm not really sure what it means to set up a typical users HABTM roles relationship nor do I really understand what a HABTM relationship is. Can anyone explain it really…
6
votes
2 answers

Rails 3 HABTM find by record associated model attribute

I think this is really basic but I'm horrible with SQL so I have no idea how to do it... I have a standard HABTM relationship between two models, LandUse and Photo. So I have a land_uses_photos join table, and each model has the standard macro,…
Andrew
  • 42,517
  • 51
  • 181
  • 281
6
votes
2 answers

Complete course and modules using Rails 5 assign to user

Edit #2 Here is the courses controller class CoursesController < ApplicationController layout proc { user_signed_in? ? "dashboard" : "application" } before_action :set_course, only: [:show, :edit, :update, :destroy] before_action…
6
votes
1 answer

Conditions to paginate for belongsToMany CakePHP 3

I have the tables Semesters, Disciplines and a jointTable Semesters_Disciplines. I want to create a action index in DisciplinesController with a semester_id as parameter, which list with paginate just the disciplines what belongs to the semester…
Luiz
  • 105
  • 1
  • 7
6
votes
1 answer

How to get array with rows related to another table for Bllim Datatable - Laravel

I use Bllim/Datatables package for the datatables on my web application; but I can't retrieve all the related rows. I use the code below: $books= $client->books()->where('books.type', 0); And I send it to Datatables::of method as follow: return…
6
votes
1 answer

Rails 4 HABTM custom validation on associations

I've got a simple scenario, but I can't seem to find any proposed solutions that apply to Rails 4. I want to simply add a custom validator that checks the amount of stored associations between my HABTM association. Easier said and done, to my…