Questions tagged [has-many-through]

1523 questions
35
votes
4 answers

Rails RSpec Tests for a has_many :through Relationship

I'm new to testing and rails but i'm trying to get my TDD process down properly. I was wondering if you use any sort of paradigm for testing has_many :through relationships? (or just has_many in general i suppose). For example, i find that in my…
Mario Zigliotto
  • 8,315
  • 7
  • 52
  • 71
33
votes
2 answers

rails has_many :through - Is it possible to have a conditions in the through table?

There are 2 models, and they are linked using a has_many :though relation. There is the :conditions parameter, that will look for a condition in the other model table, but is there someway to create a condition in the join table? For example, supose…
Tiago
  • 2,966
  • 4
  • 33
  • 41
29
votes
7 answers

accepts_nested_attributes_for with has_many => :through Options

I have two models, links and tags, associated through a third, link_tags. The following code is in my Link model. Associations: class Link < ActiveRecord::Base has_many :tags, :through => :link_tags has_many :link_tags …
Andrew C
  • 431
  • 1
  • 5
  • 8
29
votes
4 answers

How to use callbacks in a has_many through association?

I have a Task model associated to a Project model via has_many through and need to manipulate the data before delete/insert via the association. Since "Automatic deletion of join models is direct, no destroy callbacks are triggered." i can not use…
tonymarschall
  • 3,862
  • 3
  • 29
  • 52
28
votes
3 answers

Rails has_many :through nested form

I have just jumped into has_many :through association. I'm trying to implement the ability to save data for all 3 tables (Physician, Patient and association table) through a single form. My migrations: class CreatePhysicians <…
S R
  • 674
  • 2
  • 18
  • 45
25
votes
1 answer

has_many :through with a foreign key?

I've read multiple questions about this, but have yet to find an answer that works for my situation. I have 3 models: Apps, AppsGenres and Genres Here are the pertinent fields from each of…
Shpigford
  • 24,748
  • 58
  • 163
  • 252
20
votes
5 answers

How to Implement a Friendship Model in Rails 3 for a Social Networking Application?

I'm currently working on a small social networking application and right now I'm trying to create a model that represents friendships between users. This is what I came up with so far: class User < ActiveRecord::Base # ... has_many…
t6d
  • 2,595
  • 3
  • 26
  • 42
20
votes
5 answers

Filtering child objects in a has_many :through relationship in Rails 3

Greetings, I have an application where Companies and Users need to belong to each other through a CompanyMembership model, which contains extra information about the membership (specifically, whether or not the User is an admin of the company, via a…
Michelle Tilley
  • 157,729
  • 40
  • 374
  • 311
19
votes
2 answers

RoR nested :include to include sub-resources in to_xml/to_json

I have a weird data model situation to start with, so maybe my whole approach is wrong. Here's what I'm doing: I have a class called Bird and a simple class called Color. Conceptually, each bird has two to_many associations to Color, one for male…
CharlieMezak
  • 5,999
  • 1
  • 38
  • 54
18
votes
1 answer

ActiveRecord::HasManyThroughAssociationNotFoundError in UserController#welcome

I have a many to many relationship in rails. All database tables are named accordingly and appropriately. All model files are plural and use underscore to seperate words. All naming comventions are followed by ruby and rails standards. I'm using…
17
votes
1 answer

Rails: Cannot have a has_many :through association before association is defined

Upgrading an app from Rails 4.2.9 to Rails 5.2.1. Through much of the nasty part updating dependencies & whatnot and finally have app running in the console and now trying to hit pages on server. Some pages load but others: Cannot have a has_many…
Meltemi
  • 37,979
  • 50
  • 195
  • 293
17
votes
4 answers

Rails 4 Could not find the association has_many, through: relationship error

Right. This simply refuses to work. Been at this for hours. album model class Album < ActiveRecord::Base has_many :features, through: :join_table1 end features model class Feature < ActiveRecord::Base has_many :albums, through:…
Starkers
  • 10,273
  • 21
  • 95
  • 158
16
votes
3 answers

Rails has_many through form with checkboxes and extra field in the join model

I'm trying to solve a pretty common (as I thought) task. There're three models: class Product < ActiveRecord::Base validates :name, presence: true has_many :categorizations has_many :categories, :through => :categorizations …
ok32
  • 1,311
  • 12
  • 27
15
votes
3 answers

using has_many :through and build

i have three models, all for a has_many :through relationship. They look like this: class Company < ActiveRecord::Base has_many :company_users, dependent: :destroy has_many :users, through: :company_users accepts_nested_attributes_for…
rm-rf
  • 1,313
  • 1
  • 15
  • 24
14
votes
2 answers

Rails: ActiveRecord::HasManyThroughSourceAssociationNotFoundError: Could not find the source association(s)

I have the following code (somewhat simplified ... create_table :signatures do |t| t.integer :signer_id t.integer :card_id t.timestamps end With the models looking like ... class Signature < ActiveRecord::Base belongs_to :card …
slabounty
  • 704
  • 11
  • 21