Questions tagged [has-many-through]

1523 questions
3
votes
1 answer

Rails namespaced has_many through missing column

In my rails project I have a namespaced has_many through relation Scheme models/school/contact.rb class School::Contact < ApplicationRecord belongs_to :school, class_name: '::School' has_many :school_contact_emails, class_name:…
3
votes
2 answers

Rails - modeling multiple many to many relationships

I have the following use cases for creating an app that handles courses; Class A is taught by Curt in Bos on 11/1 Class A is taught by Curt in NY on 10/19 Class A is taught by Jane in SF on 12/5 Class A is taught by Jane in Bos on 11/1 What's the…
3
votes
2 answers

why does column trades.item_id not exist?

I have a relationship model in which two Users can enter into a Trade for the exchange of two Items. class User < ActiveRecord::Base has_many :owned_items, class_name: "Item" has_many :trades_received, class_name: "Trade", through: :owned_items,…
calyxofheld
  • 1,538
  • 3
  • 24
  • 62
3
votes
3 answers

Rails 5 - how to dynamically add to the form a nested object using autocomplete

I have 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 team with autocomplete input. app/models/user.rb class User < ApplicationRecord has_many…
3
votes
1 answer

Seemingly redundant ActiveRecord has_many definition

I've been applying has and belongs to many style associations to a project I've been working on, but because I wanted to capture extra information in my join model, I'm implicitly setting it up via the more elaborate belongs_to/has_many approach…
Joost Schuur
  • 4,417
  • 2
  • 24
  • 39
3
votes
1 answer

How do I save and update attributes in a join table in Rails 4 HMT association?

I have a has_many through join table setup for a recipe app where Ingredient and Meal connect through MealIngredient. Within MealIngredient, I have meal_id, ingredient_id, and amount. My question is: How can I save and update the amount column in…
don_Bigote
  • 896
  • 7
  • 33
3
votes
3 answers

How to validate presence of associated records in case of has_many, through: association?

There are models with has has_many through association: class Event < ActiveRecord::Base has_many :event_categories has_many :categories, through: :event_categories validates :categories, presence: true end class EventCategory <…
y.bregey
  • 1,469
  • 1
  • 12
  • 21
3
votes
2 answers

Rails 4 nested form with has_many, through and multiple select

I have a problem with nested form and has_many relation. Bussiness case: there are laboratories and their suppliers. Suppliers can be shared between labs. Models class Lab < ActiveRecord::Base has_many :lab_suppliers has_many :suppliers,…
3
votes
0 answers

Multiple has_many association through same table in Rails

I have following database schema: I want to be able to do something like this: dog.head << Feature.new(...) dog.tail << Feature.new(...) I am new to Rails, so I am not always sure by 100% what I am writing, but I tried following declaration of…
spacemonkey
  • 19,664
  • 14
  • 42
  • 62
3
votes
2 answers

How to access ':has_many :though' join table data when using to_json?

I have three models (simplified here): class Child < ActiveRecord::Base has_many :childviews, :dependent => :nullify has_many :observations, :through => :childviews end class Childview < ActiveRecord::Base belongs_to :observation …
qryss
  • 642
  • 1
  • 5
  • 9
3
votes
1 answer

Obtaining records where a certain condition is satisfied with two has_many :through

Let's say I have 3 models in my Rails app... Establishment, WasteType and EstablishmentWaste... My problem is that I want to get all establishments associated with a certain wastetype. This normally would be done with…
Patricio Sard
  • 2,092
  • 3
  • 22
  • 52
3
votes
2 answers

Rails 4 + Pundit : join model authorization in has_many :through association

In my Rails app, there are 3 models, defined by a has_many :through association: class User < ActiveRecord::Base has_many :administrations has_many :calendars, through: :administrations end class Calendar < ActiveRecord::Base has_many…
3
votes
1 answer

Rails 4: routing for has_many :through association

I have three models: class User < ActiveRecord::Base has_many :administrations has_many :calendars, through: :administrations end class Calendar < ActiveRecord::Base has_many :administrations has_many :users, through:…
3
votes
1 answer

Sortable List with Rails Namespaces & Railscasts 147 Error: Invalid request: Invalid HTTP format, parsing fails

I am building a sortable functionality for a join table in Rails. I have 3 models, Food, User, User_Food. Food is a prepopulated table of common foods. A user adds their favorites to User_Food. I am trying to let the user use JQuery Sortable to…
3
votes
3 answers

rails eager load has_many through join table

My development environment is on Rails 4.1 and postgresql I've 3 models with has_many through relationship: class Item < ActiveRecord::Base has_many :item_parts has_many :parts, through: :item_parts end class Part < ActiveRecord::Base …