Questions tagged [has-many-through]
1523 questions
14
votes
4 answers
Rails: Why "has_many ..., :through => ..." association results in "NameError: uninitialized constant ..."
To express that a group can have multiple users, and a user can belong to multiple groups, I set the following associations:
class Group < ActiveRecord::Base
has_many :users_groups
has_many :users, :through => :users_groups
end
class User <…

Misha Moroshko
- 166,356
- 226
- 505
- 746
14
votes
4 answers
has_many through additional attributes
How do we set additional parameters in has_many through associations?
Thanks.
Neelesh

Neelesh
- 475
- 3
- 16
14
votes
1 answer
How to access fields in a customized many-to-many through object in templates
Consider the following models:
class Person(models.Model):
name = models.CharField(max_length=128)
class Group(models.Model):
name = models.CharField(max_length=128)
members = models.ManyToManyField(Person, through='Membership')
class…

Jonathan Livni
- 101,334
- 104
- 266
- 359
14
votes
3 answers
Rails 3: validate presence of at least one has many through association item
I have two models: Project and ProjectDiscipline:
class Project < ActiveRecord::Base
has_many :project_disciplinizations, :dependent => :destroy
has_many :project_disciplines, through: :project_disciplinizations
attr_accessible…

leonel
- 10,106
- 21
- 85
- 129
13
votes
3 answers
Need data from rails join table, has_many :through
I have 3 tables - users, things, and follows. Users can follow things through the follows table, associating a user_id with a things_id. This would mean:
class User
has_many :things, :through => :follows
end
class Thing
has_many :users,…

Matt Gaidica
- 554
- 5
- 14
13
votes
1 answer
Rails has_many with `through` option "loses" joins?
I have the following example model structure:
class Category < ActiveRecord::Base
has_many :posts
scope :active, -> { where(active: true) }
end
class User < ActiveRecord::Base
has_many :posts
has_many :visible_posts, -> {…

Steve Beer
- 1,318
- 12
- 16
13
votes
3 answers
Simple_Form Association with has_many :through extra field
I have two models, Developers and Tasks,
class Developer < ActiveRecord::Base
attr_accessible :address, :comment, :email, :name, :nit, :phone, :web
has_many :assignments
has_many :tasks, :through => :assignments
end
class Task <…

Gaspar Tovar
- 145
- 1
- 1
- 6
12
votes
2 answers
Rails has many and belongs to one
I have a User model which has many projects and a Project model which can have many users, but also belongs to a single user (ie the user who created this project). It must belong to a User. It also allows a list of users to be associated with it,…

Lee Jarvis
- 16,031
- 4
- 38
- 40
12
votes
3 answers
In rails, how to destroy a 'join table item' with out deleting the real record?
I get confuse now, I don't know how to delete/destroy a record in a join table:
class Task < ActiveRecord::Base
belongs_to :schema
belongs_to :to_do
end
class Todo < ActiveRecord::Base
belongs_to :schema
has_many :tasks
end
class Schema <…

Croplio
- 3,433
- 6
- 31
- 37
12
votes
2 answers
Rails 4.0.3 Active-Admin has_many checkboxes not saving
I using rails 4.0.3 and am trying to set up many to many checkboxes in Active-Admin. The checkbox selections are not being saved. This is what i have
class Product < ActiveRecord::Base
has_many :categorizations
has_many :categories, :through =>…

user346443
- 4,672
- 15
- 57
- 80
12
votes
1 answer
has_many :through uninitialized constant
I've read the documentations and tons of tutorials about the has_many :through relations in Rails but I can't for the life of me get the hang of it.
I'm trying to add a group to my current_user(devise) and I have a table in between Group and User…

CaptainCarl
- 3,411
- 6
- 38
- 71
12
votes
6 answers
How do I force rails to not use a cached result for has_many through relations?
I have the following three models (massively simplified):
class A < ActiveRecord::Base
has_many :bs
has_many :cs, :through => :bs
end
class B < ActiveRecord::Base
belongs_to :a
has_many :cs
end
class C < ActiveRecord::Base
belongs_to…

Oliver
- 191
- 1
- 1
- 8
11
votes
5 answers
Ruby on Rails has_many through association objects before save
on a Ruby on Rails project I'm trying to access association objects on an ActiveRecord prior to saving everything to the database.
class Purchase < ActiveRecord::Base
has_many :purchase_items, dependent: :destroy
has_many :items, through:…

Nick
- 737
- 9
- 20
11
votes
1 answer
Using the reform gem with Rails, how do I populate a has_many :through nested model
I have a user model and role model, connected in ActiveRecord by:
has_many roles, through: :role_accounts
I want to have an "Edit User" screen that has a list of checkboxes, one for each role. Using the Reform gem (v2.1.0), this a snippet of the…

sockmonk
- 4,195
- 24
- 40
11
votes
2 answers
Rails has_many through aliasing with source and source_type for multiple types
So here is a sample class
class Company < ActiveRecord::Base
has_many :investments
has_many :vc_firms, through: :investments, source: :investor, source_type: 'VentureFirm'
has_many :angels, through: :investments, source: :investor,…

Michael K Madison
- 2,242
- 3
- 21
- 35