Questions tagged [has-many-through]
1523 questions
11
votes
1 answer
Activeadmin formtastic dynamic select
I would like to make a dynamic select option via Activeadmin's formtastic like so:
form do |f|
f.inputs "Exam Registration Details" do
f.input :user_id, :as => :select, :collection => User.where(:admin => 'false')
#selects user…

Ryan.lay
- 1,741
- 2
- 17
- 30
11
votes
3 answers
Detecting changes in a rails has_many :through relationship
I have a model that has a number of has_many, and has_many :through model relationships. For example, in my User class I have:
has_many :languages, through: :profile_languages
What I would like is to be able to detect when these are added or removed…

MR-RON
- 397
- 2
- 13
10
votes
2 answers
How to populate fields in a has_many through join table
I have a question concerning active record association, referring to this part of the rails documentation:
http://guides.rubyonrails.org/association_basics.html#the-has_many-through-association
if we have three models:
class Physician <…

Gnagno
- 597
- 1
- 6
- 18
10
votes
1 answer
Rails 5 - how to dynamically add nested fields in the edit form?
Already a month trying to solve the problem at first sight is not very complicated:
There are 3 models - team, user and team_user (has_namy: through)
In the form of edit and new team to do the ability to dynamically add members of this…

Nikolay Lipovtsev
- 657
- 1
- 6
- 15
10
votes
2 answers
Setting a :has_many :through association on a belongs_to association Ruby on Rails
I have three models, each having the following associations:
class Model1 < ActiveRecord::Base
has_many :model2s
has_many :model3s
end
class Model2 < ActiveRecord::Base
belongs_to :model1
has_many :model3s, :through => :model1 # will this…

Rohit
- 5,631
- 4
- 31
- 59
10
votes
2 answers
HasManyThrough with polymorphic and many-to-many relations
In my Laravel application I have the following classes:
class Product extends Model
{
public function extended()
{
return $this->morphTo();
}
public function users {
return $this->belongsToMany('App\User',…

Oskar Persson
- 6,605
- 15
- 63
- 124
10
votes
4 answers
Rails/ActiveRecord has_many through: association on unsaved objects
Let's work with these classes:
class User < ActiveRecord::Base
has_many :project_participations
has_many :projects, through: :project_participations, inverse_of: :users
end
class ProjectParticipation < ActiveRecord::Base
belongs_to…

Markus
- 5,667
- 4
- 48
- 64
10
votes
1 answer
Getting ActiveRecord::RecordInvalid error with has_many through association; validation issue on join table
I have three associated models like these:
class Product < ActiveRecord::Base
belongs_to :user
has_many :descriptions, {
dependent: :destroy,
before_add: [:add_user_id_to_description, :validate_description]
}
has_many :documents,…

Jacob Brown
- 7,221
- 4
- 30
- 50
10
votes
1 answer
find_or_create on a has many through relationship
I have a has many through relationship in my app:
Shows has many Bands through => Lineups
Bands are unique by :name
class Show < ActiveRecord::Base
attr_accessible :city_id, :title, :dateonly, :timeonly, :image, :canceled, :venue_attributes,…

wiredin
- 269
- 3
- 10
10
votes
1 answer
How can I create new records with has_many :through and honor :conditions?
Let's say I have a Course in which Students can enroll via a Membership (e.g. a has_and_belongs_to_many relationsip of Courses and Students). Some memberships are for students who are just observing the class (not for credit, etc.), so:
class…

Eric
- 935
- 1
- 8
- 23
10
votes
1 answer
has_many :through NameError: uninitialized constant
I just want to make a little join table, eventually storing extra info on that join (which is why I'm not using HABTM). From the rails documentation of associations I've created the following models:
class Physician < ActiveRecord::Base
has_many…

Neil
- 852
- 1
- 8
- 14
9
votes
2 answers
multiple database connections with has_many through
How can I make a has_many through work with multiple database connections?
I have a database named "master" that holds the location information. That is updated from a separate application. Users can have access to many locations, but all the…

Toby Joiner
- 4,306
- 7
- 32
- 47
9
votes
1 answer
Saving the order of associated records in a Rails has_many :through association
I'm working on a Rails plugin that includes a way to modify the order of associated records in a has_many :through association. Say we have the following models:
class Playlist < ActiveRecord::Base
has_many :playlists_songs, :dependent =>…

Tom
- 1,007
- 12
- 13
9
votes
2 answers
Destroy associations after the last has_many :through record is deleted
With a regular has_many, there's the option of :dependent => :destroy to delete the associations when the parent record is deleted. With has_many :through, there might be other parents associated to the child records, so :dependent => :destroy…

Andrew Vit
- 18,961
- 6
- 77
- 84
9
votes
2 answers
Rails: ActiveRecord Query for has_many :through model
How to query for Companies with a certain Branch in a "has_many :through" relationship?
#company.rb
has_many :branch_choices
has_many :branches, :through => :branch_choices
"Find all companies with Branch ID 3"

David
- 937
- 1
- 12
- 22