Questions tagged [has-many-through]
1523 questions
3
votes
0 answers
has_many:through pass extra attributes value for the joined model
Here is my model definition
class Partner < ActiveRecord::Base
has_many :projectcollaborations
has_many :projects, :through => :projectcollaborations, :source => :project
end
class Project < ActiveRecord::Base
has_many…

Kamrul Hassan
- 294
- 1
- 2
- 13
3
votes
3 answers
Avoiding `save!` on Has Many Through Association
I have a has_many through association with an attribute and some validations on the "join model". When I try to do something like @user.projects << @project and the association has already been created (thus the uniqueness validation fails), an…

coreyward
- 77,547
- 20
- 137
- 166
3
votes
1 answer
How can I build a 'has-many-through' relation linking more than 2 models?
I have 3 models eg;
TABLE `users`
`id` INT
`username` VARCHAR(32)
...
TABLE `books`
`id` INT
`title` VARCHAR(100)
`author` INT (foreign ket constraint)
TABLE `rights`
`id` INT
`name` VARCHAR(32)
Now I want a user…

giorgio
- 10,111
- 2
- 28
- 41
3
votes
1 answer
Is there a Rails way or a gem to get related entries from HABTM to the same object?
Well, I have a table with images, each image is tagged with a HABTM relation through a join table.
What I want to do is show related images in the page the image is being shown, by matching images with the same tags.
Can this be done efficiently…

Zequez
- 3,399
- 2
- 31
- 42
3
votes
1 answer
Rails: has_many through association - did I get this right?
I building a photo sharing web application using Rails 3.1. I just want to verify that I got the associations right.
Some context: A User has many Share. A Share has one User (i.e the "sharer"), one Photoand many Receiver. A Receiveris a arbitrary…
user230017
3
votes
1 answer
has_many :through with :foreign_key
My models look like this:
class Post < ActiveRecord::Base
has_many :aspect_visibilities, :as => :shareable, :primary_key => :guid, :foreign_key => :shareable_guid
has_many :aspects, :through => :aspect_visibilities
end
class AspectVisibility <…

manuels
- 1,511
- 3
- 14
- 26
3
votes
2 answers
has_many through build
I have two models. User and Account as follows
class Account < ActiveRecord::Base
has_many :manages
has_many :users, :through => :manages
end
class User < ActiveRecord::Base
has_many :manages
has_many :accounts, :through => :manages
end
If…

Edward Huynh
- 2,907
- 1
- 27
- 26
3
votes
1 answer
has_one :through => multiple
Both Attendment & Vouching:
belongs_to :event
belongs_to :account
Therefore: 1 to 1 relationship between attendments and vouchings.
Is there a way to do this without my thinking too much?
# attendment
has_one :vouching :through => [:event,…

Peter Ehrlich
- 6,969
- 4
- 49
- 65
3
votes
2 answers
find_or_initialize_by and has_many :through
I have an xml file the user imports data from. This creates a record in the Player model.
At the same time, I want a record to be created in the Membership association.
Both of the above should only be triggered if the record does not already…

amunds
- 702
- 8
- 22
3
votes
2 answers
acl9 has_many through implementation
I got the following deprecation warning on the rails console:
DEPRECATION WARNING: Having additional attributes on the join table of a
has_and_belongs_to_many association is deprecated and will be removed in Rails 3.1.
Please use a has_many…

Ian
- 369
- 1
- 2
- 11
3
votes
1 answer
ruby on rails has_many :through association which has two columns with same model
I have the following model:
class UserShareTag < ActiveRecord::Base
attr_protected :sharee_id, :post_id, :sharer_id
belongs_to :sharer, :class_name => "User"
belongs_to :post
belongs_to :sharee, :class_name => "User"
validates…

deruse
- 2,851
- 7
- 40
- 60
3
votes
1 answer
My "has_many through" join model has nil reference after saving
I'm trying to create an object and adding an existing object to a "has_many through" association, but after saving my object the reference to my newly created object is set to nil in the join model.
To be specific, I'm creating a Notification…

Lars Preben Sørsdahl
- 211
- 1
- 11
3
votes
2 answers
Rails 5 hasMany through is not filtering
I am migrating from Rails 4 to Rails 5 and got into this problem. I have the hasMany through relationship to connect the Track and the RightHolder classes:
class RightHolder < ActiveRecord::Base
has_many :right_holder_tracks, class_name:…

Daniel Cukier
- 11,502
- 15
- 68
- 123
3
votes
0 answers
Rails has_many, :through form adding variables to joining table
I'm trying to build an app for storing recipes so that I can (eventually) build shopping lists based on recipe ingredients.
What I'm struggling with is being able to link ingredients to recipes based on their measures, i.e. a recipe could use 300…

Cat Burston
- 2,833
- 2
- 12
- 10
3
votes
1 answer
Laravel hasManyThrough a polymorphic relation
I have a table with transactions where every Transaction belongs to either a Driver, or a Customer - so I set a polymorphic relation between them.
For Transaction I have set:
public function owner() {
return $this->morphTo();
}
For Driver and…

Zlautumn
- 89
- 1
- 11