Questions tagged [has-many-through]

1523 questions
7
votes
3 answers

rspec testing has_many :through and after_save

I have an (I think) relatively straightforward has_many :through relationship with a join table: class User < ActiveRecord::Base has_many :user_following_thing_relationships has_many :things, :through =>…
dantswain
  • 5,427
  • 1
  • 29
  • 37
7
votes
2 answers

Creating controllers and views for a has_many :through relationship in Rails 3

There are many tutorials that show you how to create the model instructions for a has_many :through relationship in Rails, but there doesn't seem to be many articles related to the process of setting up forms to create and edit these relationships.…
Randy Burgess
  • 4,835
  • 6
  • 41
  • 59
7
votes
1 answer

Rails has_many :through with conditions and building associations

I'm having problems building an association that is a has_many :through with conditions. I have this model: class Contact < AR has_many :group_contacts has_many :groups, :through => :group_contacts, :conditions => {:groups => {:published =>…
brad
  • 31,987
  • 28
  • 102
  • 155
7
votes
2 answers

Default_scope on a join table

I've got a model setup like the following: class User has_many :items has_many :words, :through => :items end class Item belongs_to :user belongs_to :word default_scope where(:active => true) end class Words has_many :items end The…
7
votes
1 answer

has_many :through, :source, :source_type returning empty array

I got some Manager and SoccerTeam model. A manager "owns" many soccer teams; also a manager can comment on soccer teams and can comment on other managers too: manager.rb # Soccer teams the manager owns has_many :soccer_teams, :dependent =>…
7
votes
1 answer

How to create a form for hasManyThrough relationship, with pivot, in a Laravel5 view

I am coming from a Rails background where we can create has_many_through pretty easily with the right references. My models in this project: Deal <=> DealsFaqs <=> Faqs In the Deals model I have: public function faqs(){ return…
TJ Sherrill
  • 2,465
  • 7
  • 50
  • 88
7
votes
1 answer

Rails 4: combining has_many :through association with polymorphic association

In my Rails 4 app, I have the following models: User has_many :administrations has_many :calendars, through: :administrations has_many :comments has_many :calendar_comments, through: :calendars, :source => :comments Calendar has_many…
7
votes
4 answers

How do I use accepts_nested_attributes_for?

Editing my question for conciseness and to update what I've done: How do I model having multiple Addresses for a Company and assign a single Address to a Contact, and be able to assign them when creating or editing a Contact? I want to use nested…
Satchel
  • 16,414
  • 23
  • 106
  • 192
7
votes
2 answers

Many to Many Relationships with Ember, ember-data and Rails

I am having an issue with trying to save many to many relationships in Ember.js using ember-data and rails. The association works fine on the ember side of things, however when I try to commit the transaction it will not include the list of new…
7
votes
3 answers

Django: prefetch_related results ordered by a field of an intermediary table

How can I prefetch_related objects in Django and order them by a field in an intermediary table? Here's the models I'm working with: class Node(models.Model): name = models.CharField(max_length=255) edges = models.ManyToManyField('self',…
Ollie Glass
  • 19,455
  • 21
  • 76
  • 107
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
6
votes
2 answers

ruby on rails after_remove, after_add callbacks on has_many :through

I have a model which fits the following pattern: class foo < ActiveRecord::Base has_many :bar, :dependent => :destroy has_many :baz, :through => :bar, :uniq => true, :after_add => :update_baz_count, :after_remove => :update_baz_count …
deruse
  • 2,851
  • 7
  • 40
  • 60
6
votes
1 answer

Please explain the has_many, through: source: Rails Association

I've found a bunch of articles, stackoverflow answers and rails documentation about 'source:', but none of it explains this association in a way I can understand it. I need the most simplified explanation of this way of associating, if possible. My…
6
votes
1 answer

how to order of has_many through by a specific field

I have 3 models: member, team, and team_enrollment. The structure is as follows: class Member < ApplicationRecord has_many :team_enrollments has_many :teams, -> {order 'team_enrollments.termination_date DESC NULLS LAST'}, through:…
6
votes
2 answers

How do you get a list from complex active record has_many involved from a subset of downstream objects

I'm having a hard time getting a list of the games involved from a hierarchical parent relationship when multiple foreign keys are implemented on a relationship in the middle. Given League Object NFC, find all of its Game objects [G1,G3,G4] # id …
shadowbq
  • 1,232
  • 1
  • 16
  • 29