Questions tagged [has-many-through]

1523 questions
5
votes
1 answer

Adding and removing from a has_many :through relation

From the Rails associations guide, they demonstrate a many-to-many relationship using has_many :through like so: class Physician < ActiveRecord::Base has_many :appointments has_many :patients, :through => :appointments end class Appointment <…
dteoh
  • 5,794
  • 3
  • 27
  • 35
5
votes
4 answers

Elegantly selecting attributes from has_many :through join models in Rails

I'm wondering what the easiest/most elegant way of selecting attributes from join models in has_many :through associations is. Lets say we have Items, Catalogs, and CatalogItems with the following Item class: class Item < ActiveRecord::Base …
5
votes
1 answer

Rails Has Many Through Polymorphic Checkboxes

This one's really getting me down! :( I'm trying to make a nested model form for my User model with a checkbox list in it where multiple Stores can be checked or unchecked to administer the Stores through model Staffing. class Staffing <…
5
votes
1 answer

Right way to force uniqueness on a join model? (has_many :through)

I have a parent/child relationship via our users table, with models as such: class User < ActiveRecord::Base # Parents relationship has_many :children_parents, :class_name => "ParentsChild", :foreign_key => "child_id", :dependent => :destroy …
Brett Bender
  • 19,388
  • 2
  • 38
  • 46
5
votes
1 answer

How to build a model form that has has_many relations on its own

I have a Phrase class that has_many Phrase as Translation. app/models/phrase.rb class Phrase < ActiveRecord::Base has_many :translatabilities has_many :translations, through: :translatabilities has_many :inverse_translatabilities, class_name:…
ironsand
  • 14,329
  • 17
  • 83
  • 176
5
votes
2 answers

Why doesn't collection=objects on has_many :through trigger callbacks on the join model on deletion of the association?

The Rails 4 documentation says this regarding destroy callbacks on the join model for a has_many :through relationship: collection=objects Replaces the collections content by deleting and adding objects as appropriate. If the :through option is…
5
votes
1 answer

Rails and Globalize - join translation table of related model in scope

I want to order some records of a model that has a relation to another model (with translated attributes). Here an example: I have a model Project I have a model Task I have the relation Project has_many Tasks The model Task has attribute name…
5
votes
2 answers

Rails 3 can't perform validation for persited object when use collection_singular_ids=ids method

Is there any way to avoid automatically saving object while assigning collection attributes(collection_singular_ids=ids method)? for example, I have the following Test and Package model, Package has many tests. User can build package bundle with…
5
votes
0 answers

How to combine collection_check_boxes with additional fields in a "has_may through" form?

I have models that look something like this: class Team has_many :users, through: :team_members end class User has_many :teams, through: :team_members end class TeamMember belongs_to :team belongs_to :user # with a boolean attribute of…
Andrew
  • 227,796
  • 193
  • 515
  • 708
5
votes
1 answer

Create and update with nested models using strong parameters Rails

Here are my 3 models. User has_many :memberships has_many :teams, through: :memberships, dependent: :destroy accepts_nested_attributes_for :memberships Team has_many :memberships has_many :users, through: :memberships,…
5
votes
1 answer

Active Admin has_many selectable list of records

I have been trying for days now, I am new at ROR and active admin. So far I have been able to add and delete has_many relations for a new record. And I am using strong_parameters as well as accept_nested_attributes. I want the Ability to add and…
Pinser
  • 1,898
  • 3
  • 29
  • 43
5
votes
2 answers

Rails class name/type not working for a polymorphic has_many :through

I have an invoicing system that manages debits and credits. Basically the invoice amount is obtained by the sum of its debits and the balance is derived by taking the sum of its credits and subtracting it against the total amount. I'm doing this…
Jim Jeffers
  • 17,572
  • 4
  • 41
  • 49
5
votes
2 answers

Creating multiple has_many through associations from an array of IDs

I have models User, Photo and Favorite, where favorites is a join table from users to photos, i.e.: class User < ActiveRecord::Base has_many :favorites has_many :photos, through: `favorites` end class Photo < ActiveRecord::Base has_many…
GMA
  • 5,816
  • 6
  • 51
  • 80
5
votes
3 answers

Establishing A Has One Through vs Has Many Through With Unsaved Models

I have written a gem that allows Google Spreadsheets to be transformed into Rails models. The sequence of this process involves creating all the models, then hooking up their associations, then saving all the models. It supports all the types of…
5
votes
2 answers

Wrong order on has_many association when upgrading from rails 3 to rails 4

I am trying to update one project from Rails 3 to Rails 4. In Rails 3 I was doing: class Sale < ActiveRecord::Base has_many :windows, :dependent => :destroy has_many :tint_codes, :through => :windows, :uniq => true, :order => 'code ASC' …
st3fan
  • 1,630
  • 14
  • 32