Questions tagged [has-many-through]
1523 questions
108
votes
3 answers
how to add records to has_many :through association in rails
class Agents << ActiveRecord::Base
belongs_to :customer
belongs_to :house
end
class Customer << ActiveRecord::Base
has_many :agents
has_many :houses, through: :agents
end
class House << ActiveRecord::Base
has_many :agents
has_many…

Mike
- 1,361
- 2
- 13
- 13
102
votes
7 answers
How to set up factory in FactoryBot with has_many association
Can someone tell me if I'm just going about the setup the wrong way?
I have the following models that have has_many.through associations:
class Listing < ActiveRecord::Base
attr_accessible ...
has_many :listing_features
has_many :features,…

Tonys
- 3,349
- 4
- 24
- 27
72
votes
4 answers
Rails has_many :through Find by Extra Attributes in Join Model
New to both Ruby and Rails but I'm book educated by now (which apparently means nothing, haha).
I've got two models, Event and User joined through a table EventUser
class User < ActiveRecord::Base
has_many :event_users
has_many :events, :through…

Stefan Mai
- 23,367
- 6
- 55
- 61
64
votes
4 answers
Ruby-on-Rails: Multiple has_many :through possible?
Is it possible to have multiple has_many :through relationships that pass through each other in Rails? I received the suggestion to do so as a solution for another question I posted, but have been unable to get it to work.
Friends are a cyclic…

William Jones
- 18,089
- 17
- 63
- 98
62
votes
8 answers
How do I order a has_many through association in Ruby on Rails?
Given the following AR models, I would like to sort users alphabetically by last name when given a handle to a task:
#user
has_many :assignments
has_many :tasks, :through => :assignments
#assignment
belongs_to :task
belongs_to…

rswolff
- 3,258
- 5
- 28
- 31
57
votes
3 answers
dependent => destroy on a "has_many through" association
Apparently dependent => destroy is ignored when also using the :through option.
So I have this...
class Comment < ActiveRecord::Base
has_many :comment_users, :dependent => :destroy
has_many :users, :through => :comment_users
...
end
...but…

blogofsongs
- 2,527
- 4
- 21
- 26
54
votes
2 answers
Rails: has_many through with polymorphic association - will this work?
A Person can have many Events and each Event can have one polymorphic Eventable record. How do I specify the relationship between the Person and the Eventable record?
Here are the models I have:
class Event < ActiveRecord::Base
belongs_to…

Finch
- 1,741
- 3
- 15
- 15
48
votes
1 answer
Active Record has_many :through remove one associated record
This may be a very basic oversight on my part, but I can't seem to recall a simple method for removing an association between two objects joined via has_many :through. IE:
class Photo
has_many :tags, :through => :taggings
has_many :taggings,…

Andrew
- 42,517
- 51
- 181
- 281
47
votes
3 answers
Validate that an object has one or more associated objects
I need to ensure that when a product is created it has atleast one category.
I could do this with a custom validation class, but I was hoping there was a more standard way of doing it.
class Product < ActiveRecord::Base
has_many…

recursive_acronym
- 2,981
- 6
- 40
- 59
45
votes
2 answers
Rails Associations - has_many => :through - but same model
What I am trying to do:
I have a blog and want to show related posts below the main post.
class Post < ActiveRecord::Base
has_many :related_posts
has_many :posts, :through => :related_posts
end
And then in the join model/table
class…

thenengah
- 42,557
- 33
- 113
- 157
44
votes
3 answers
Rails: HasManyThroughAssociationNotFoundError
I have problems with getting a has_many through association to work.
I keep getting this exception:
Article.find(1).warehouses.build
ActiveRecord::HasManyThroughAssociationNotFoundError: Could not find the association :entries in model…
mcfoobar
40
votes
9 answers
how to avoid duplicates in a has_many :through relationship?
How can I achieve the following? I have two models (blogs and readers) and a JOIN table that will allow me to have an N:M relationship between them:
class Blog < ActiveRecord::Base
has_many :blogs_readers, :dependent => :destroy
has_many…

Sebastian
- 2,889
- 4
- 34
- 37
40
votes
3 answers
Rails 4 Form: has_many through: checkboxes
Original question
Two resources: users and animals. When creating a user, the client selects check boxes to say how many animals they have.
When the user form is submitted, it should not only create a new user record, but it should also create a…

Neil
- 4,578
- 14
- 70
- 155
39
votes
4 answers
Laravel Polymorphic Relations Has Many Through
I have a Subscriber model
// Subscriber Model
id
user_id
subscribable_id
subscribable_type
public function user()
{
return $this->belongsTo('App\User');
}
public function subscribable()
{
return $this->morphTo();
}
And a Topic model
//…

Edward
- 421
- 1
- 4
- 5
37
votes
6 answers
Rails - Sort by join table data
I've got a RoR project in the works. Here are the applicable sections of my models.
Home
has_many :communities, :through => :availabilities
has_many :availabilities, :order => "price ASC"
Community
has_many :homes, :through =>…

Steve Davis
- 959
- 1
- 11
- 25