Questions tagged [has-many-through]

1523 questions
6
votes
2 answers

rails 3 has_many :through record save error

I'm not exactly sure what my problem is, so this question may require some more clarification, but here's what seems to be most relevant: I have a has_many :through and the join model has some fields that aren't foreign keys. When I build the…
SooDesuNe
  • 9,880
  • 10
  • 57
  • 91
6
votes
1 answer

Rails 4 collection_check_boxes, with a has_many through

I'm trying to associate categories to products. The way I've implemented it so far is Class Product has_many :categorizations has_many :categories, through: :categorizations . Class Categorization belongs_to :product belongs_to…
6
votes
1 answer

How can one obtain a row count from has_many :through relations with :uniq => true

This is my model: class Tag < ActiveRecord::Base # id, name has_many :taggings end class Tagging < ActiveRecord::Base # id, tag_id, owner_id, target_type, target_id belongs_to :tag belongs_to :owner, :class_name => 'User' belongs_to…
Noel Walters
  • 1,843
  • 1
  • 14
  • 20
6
votes
2 answers

Specifying the foreign key in a has_many :through relationship

I have the following three models: User, Project, and Assignment. A User has_many Projects through an assignment. However, Assignment actually has two foreign keys that relate to a User: user_id (representing the user who was assigned the project)…
jakeboxer
  • 3,300
  • 4
  • 26
  • 27
6
votes
1 answer

has_many :through questions

I was previously using has_and_belongs_to_many, and have converted to has_many :through. Here's how it looks for a list of games that can have many users playing. With this, I can do game.users and user.games....: class Game < ActiveRecord::Base …
cmaughan
  • 2,596
  • 5
  • 30
  • 35
6
votes
1 answer

Rails model with two polymorphic has_many through: associations for object tagging

My schema has Articles and Journals that can be tagged with Tags. This requires a has_many through: association with a polymorphic relationship to my Tagging join table. Okay, that's the easy and well documented part. My problem is that Articles can…
6
votes
1 answer

Editing many-to-many relations in Activeadmin

I am looking for a way to edit/add keywords related to an article, inline in Activeadmin. I have defined a simple many-to-many setup: class Area < ActiveRecord::Base has_many :area_keywords has_many :keywords, :through => :area_keywords …
6
votes
4 answers

Rails nested attributes - how to add category attribute to new product?

I'm using rails to create a new product and want to add a category to every product. I have three tables: product, category, and categorizations (which stores the relationship between products and categories). I'm trying to use nested attributes to…
william tell
  • 4,352
  • 6
  • 23
  • 27
5
votes
1 answer

Rails 3.2 - accepts_nested_attributes_for and join models

I have the following models: user, role, user_role (user_role is a join model) I am trying to edit a user's roles using checkboxes on the user#edit page. Here's my attempt, I feel like I'm missing something significant, or taking the wrong…
ardavis
  • 9,842
  • 12
  • 58
  • 112
5
votes
1 answer

Why is my has_many through associated record (sometimes) readonly?

I have three ActiveRecord models: Partner, MembershipChannel (which is an STI model, inheriting from Channel) and ChannelMembership (I was not responsible for naming these models…) When I load a ChannelMembership through the Partner association, I…
Martin Svalin
  • 2,227
  • 1
  • 17
  • 23
5
votes
4 answers

Saving join attributes through a has_many :through with :conditions

I have an Artist model that looks like this: # app/models/artist.rb class Artist < ActiveRecord::Base # Relationships has_many :releases has_many :songs, :through => :releases has_many :featured_songs, :through => :releases, …
5
votes
2 answers

Rails has_many :through with custom foreign_key

I have the following set of models: class Cardstock < ActiveRecord::Base has_many :color_matches, :primary_key => :hex, :foreign_key => :hex has_many :palette_colors, :through => :color_matches end class ColorMatch < ActiveRecord::Base …
Alex Kahn
  • 537
  • 5
  • 10
5
votes
1 answer

Rails custom ActiveRecord::Type fails when using `class_name` in has_many :through association

I'm using KSUIDs as a replacement for UUIDs in my Rails app. michaelherold/ksuid-ruby ported KSUIDs to Ruby and implemented them as ::ActiveRecord::Type::String. Everything is working great except one little bug when using has_many :through…
mattes
  • 8,936
  • 5
  • 48
  • 73
5
votes
1 answer

Creating or updating association with has_many :through

Say I have two models, Director and Movie, and a third join model called Directions. They are defined as such: Movie: class Movie < ActiveRecord::Base has_many :directions has_many :directors, :through => :directions end Director: class…
5
votes
2 answers

activerecord find through association

I am trying to retrieve an activerecord object from my db. My models are class User < ActiveRecord::Base belongs_to :account has_many :domains, :through => :account end And class Account < ActiveRecord::Base has_many :domains …
Dimitris
  • 2,501
  • 3
  • 21
  • 28