Questions tagged [has-many-through]
1523 questions
9
votes
2 answers
Has_Many :Through or :finder_sql
I've nailed down what I want, but I can't seem to get it in a way that the rails designers are looking for. Basically, I have (please set aside pluralization/etc issues):
Human
Relationships (Parent, Offspring)
I'm trying to get all the offsprings…

aronchick
- 6,786
- 9
- 48
- 75
9
votes
3 answers
HMT collection_singular_ids= deletion of join models is direct, no destroy callbacks are triggered
Just ran into an issue with a has_many :through association and after/before-destroy callbacks not being triggered.
Say, I have users, groups, and an intermediate relation called membership.
I have a form that allows users to be enrolled into…

Swartz
- 1,051
- 2
- 11
- 23
9
votes
3 answers
Rails3 nested has_many through question
We are planning to upgrade our application to Rails3. One plugin we've used quite a bit is nested_has_many_through. This plugin seems outdated, and no longer maintained, and simply does not appear to be working in a new Rails3 application.
A simple…

releod
- 513
- 5
- 12
9
votes
1 answer
After upgrade to Rails 4: Specs fail in combination while passing in isolation
I'm struggling with this for quite a while now: I'm trying to upgrade an app from Rails 3.2 to Rails 4. While on Rails 3.2 all specs are passing, they fail under certain conditions in Rails 4.
Some specs are passing in isolation while failing when…

fiedl
- 5,667
- 4
- 44
- 57
9
votes
2 answers
After upgrading to Rails 4.1, new polymorphic associations are invalid when saving them along with their parent
After upgrading from Rails 3.2 to 4.1, the following code which used to work is now failing:
in a controller/spec:
post = user.posts.build
post.contacts << contact # contact is a persisted record
post.save! # now fails
I'm basically trying to save…

Yossi Shasho
- 3,632
- 31
- 47
9
votes
3 answers
Using fields from an association (has_many) model with formtastic in rails
I searched and tried a lot, but I can't accomplish it as I want.. so here's my problem.
class Moving < ActiveRecord::Base
has_many :movingresources, :dependent => :destroy
has_many :resources, :through => :movingresources
end
class…

pdu
- 10,295
- 4
- 58
- 95
9
votes
3 answers
Rails has_many :through --> Should I keep a primary key column?
I have 'author' and 'book' tables, joined in a has_many :through table 'author_book'
As far as I can tell, there's no purpose to an :id primary key field on the 'author_book' table...but before I commit to that idea, I just wanted to confirm. So, is…

PlankTon
- 12,443
- 16
- 84
- 153
8
votes
1 answer
ActiveRecord has_many :through duplicating counter caches on mass assignment
It seems ActiveRecord's counter_cache feature can result in a counter cache being incremented twice. The scenario in which I am seeing this behavior is when I have two models that have a has_many :through relationship with each other through a join…

Carl Zulauf
- 39,378
- 2
- 34
- 47
8
votes
3 answers
Rails forms for has_many through association with additional attributes?
How can I generate form fields for a has_many :through association that has additional attributes?
The has_many :through relationship has an additional column called weight.
Here's the migration file for the join table:
create_table :users_widgets…

Marco
- 4,345
- 6
- 43
- 77
8
votes
2 answers
Rails / Papertrail: Changeset with association changes
I am stuck. I've been trying to figure out how to include the association changes (has_many, has_many through) on a model that has papertrail. I would like to call MyModel.versions.first.changeset and have any changes that took place on associated…

heinztomato
- 795
- 1
- 7
- 12
8
votes
2 answers
Why is the source_type for this polymorphic association always 0?
For some reason the source type for a polymorphic has_many :through association is always 0, despite having set a :source_type.
Here's what my models look like...
Foo:
has_many :tagged_items, :as => :taggable
has_many :tags, :through =>…

hobberwickey
- 6,118
- 4
- 28
- 29
8
votes
1 answer
CakePHP saveAssociated not saving HasMany Through Model Data
I'm trying to get my associated models in CakePHP 2.3 to save properly, but I'm having issues. I'm storing posts, and I want to know what links are in those posts. For each of those links, I'd like to store anchor text if it is available. My…

Derek Perkins
- 637
- 7
- 17
8
votes
2 answers
Rails has_many :through PG::Error: ERROR: column reference "id" is ambiguous error
I'm new to rails and I've been trying to get two has_many :though relationships to work out (as opposed to using has_and_belongs_to_many as explained by this post…

Eric Norcross
- 4,177
- 4
- 28
- 53
7
votes
2 answers
Active admin has_many through delete association
I'm currently making association like this :
show do
h3 project.title
panel "Utilisateurs" do
table_for project.roles do
column "Prenom" do |role|
role.user.firstname
end
column "Nom" do |role|
…

Awea
- 3,163
- 8
- 40
- 59
7
votes
2 answers
Dynamically create after_add and after_remove callbacks for has_many or habtm?
Is there a way to dynamically add after_add and after_remove callbacks to an existing has_many or has_and_belongs_to_many relationship?
For example, suppose I have models User, Thing, and a join model UserThingRelationship, and the User model is…

dantswain
- 5,427
- 1
- 29
- 37