Questions tagged [has-many-through]

1523 questions
5
votes
1 answer

Rails 4: checkboxes with a has_many through

I'm building an application which has to assign a assignment to multiple employers. I have build these models: #assignment.rb class Assignment < ActiveRecord::Base has_many :employer_assignments has_many :employers, :through =>…
Laurens
  • 51
  • 1
  • 3
5
votes
1 answer

Rails has_many :through association. Remove an association with a link?

I've got an Events model and a Users model joined through an Attendees model. I have figured out how to "Attend" an event as an authenticated user. But what I can't figure out is a nice way to "Withdraw" from an event. I'm sure this is something…
Brandt
  • 53
  • 1
  • 3
5
votes
3 answers

CakePHP: bi-directional self-referential hasMany Through associations

I'm trying to get my head around bi-directional self-referential hasMany through relationships in CakePHP (what a mouthful!). I'm working on a picture matching website. Pictures are associated to other pictures via a 'match' (the join model). Each…
Viento
  • 73
  • 3
4
votes
1 answer

Rails complex view form with has_many :through association

I'm trying to create a rails app for recipes, but am confused on how to create the view forms and controller logic. I have 2 models, Recipe and Item, joined in a has_many :through association with an Ingredient model as follows: class Recipe <…
4
votes
2 answers

TDD of has_many through Model validations with RSpec & Factory Girl

Consider the following: ScheduledSession ------> Applicant <------ ApplicantSignup Points to note: A ScheduledSession will exist in the system at all times; think of this as a class or course. The intent here is to validate the ApplicantSignup…
Michael De Silva
  • 3,808
  • 1
  • 20
  • 24
4
votes
2 answers

Do I need a join table for a has_many :through association?

I've only created a has_and_belongs_to_many association before and it is different from has_many :through. For a has_many :through association, do I need a join table? How does the actual association work? Do I need an index? I can't find a great…
Matthew Berman
  • 8,481
  • 10
  • 49
  • 98
4
votes
3 answers

Eager Loading with "has many through" -- do I need Arel?

I have three tables: users, members, projects. The middle is a join table expressing a has-many-through between the other two tables; and it has some attributes of interest, including join_code and activated. More expansively: class User <…
Purplejacket
  • 1,808
  • 2
  • 25
  • 43
4
votes
1 answer

Rails 3 and has_many :through: automagically set/initialize attributes on join model

I deeply searched the web in order to find a clean and simple way to deal with attributes initialization on the join model of a has_many :through relation, but I did not find a best solution for my need. In the exaple I provide below, I need to…
4
votes
1 answer

Models -> has_many -> Twice

So I have a somewhat confusing relationship here, between a Note, Group, and User. And I ended up with has_many twice in my model. But I'm currently focused on the Note & Group relationship. Background: A Group can have a note. A User can also have…
ardavis
  • 9,842
  • 12
  • 58
  • 112
4
votes
3 answers

How to detect changes in has_many through association?

I have the following models. class Company < ApplicationRecord has_many :company_users has_many :users, :through => :company_users after_update :do_something private def do_something # check if users of the company have been updated…
4
votes
4 answers

Rails: ArgumentError - you can't define an already defined column

I have a Rails 6 app (6.0.0rc1). It has book and author models that have has_many :through associations. I created a join table with rails g migration CreateJoinTableAuthorsBook author:references books:references As you can see, I named the table…
hashrocket
  • 2,210
  • 1
  • 16
  • 25
4
votes
2 answers

How to save many has_many_through objects at the same time in Rails?

I have two models related as follows. USERS has_many :celebrations has_many :boards, :through => :celebrations BOARDS has_many :celebrations has_many :users, :through => :celebrations CELEBRATIONS :belongs_to :user :belongs_to :board In my…
chell
  • 7,646
  • 16
  • 74
  • 140
4
votes
1 answer

has_many :through + polymorphic relationships

I using rails3 and trying to build some complex associations. I have Product, Version and Property models. class Version < ActiveRecord::Base belongs_to :product has_many :specs has_many :properties, :through => :specs end class Product <…
4
votes
2 answers

Getting pivot values with far relations in Laravel

In my Laravel project I've used the following (inspired from here) to get a far relation from a model to another. I can't use the HasManyThrough method since the bars and bazs are related with a polymorphic relation. public class MyClass { …
Oskar Persson
  • 6,605
  • 15
  • 63
  • 124
4
votes
2 answers

Issues with has_many :through, cache, touch and counter_cache

I have a lot of has_many :through relations in my app. I am extensivley showing informations related to this, such as number of connected objects. Whenever user updates the relation, join table is modified, and I can catch this my Sweepers. The…
mdrozdziel
  • 5,528
  • 6
  • 39
  • 55