Questions tagged [has-many-through]

1523 questions
4
votes
2 answers

Rails has_many through not returning records

I am trying to setup and use my first has_many through association in Rails 4.2. I have populated the tables with data and can see associated data across the three tables, but I can't understand if my issue is with model, associations, or table…
Lloydo
  • 303
  • 1
  • 8
4
votes
1 answer

has_many :through, not reloaded/reset

I have three models, two of them are linked through a join model: # user.rb has_many :user_cards has_many :cards, through: :user_cards # cards.rb has_many :user_cards has_many :users, through: user_cards # user_cards.rb belongs_to :user …
user4187589
4
votes
3 answers

insert data through third table

I have 3 models (Client, User and Admin) without controllers class Admin < ActiveRecord::Base validates_presence_of :username, :user_id, :client_id belongs_to :client belongs_to :user end class Client < ActiveRecord::Base …
4
votes
0 answers

Eloquent multiple has many through and inverse

(Laravel 4.2) I have the following schema A one to many B B one to many C C one to one D Now I know that hasManyThrough can be used to get A one to many C, but can I use it to get `A one to many D'? The way I am doing is right now is getting C…
4
votes
1 answer

Rails 4: counter_cache in has_many :through association with dependent: :destroy

Although similar questions have already been asked: counter_cache with has_many :through dependent => destroy on a "has_many through" association has_many :through with counter_cache none of them actually addresses my issue. I have three models,…
4
votes
0 answers

How to get acts_as_list to work with a has_many through association?

I am having trouble figuring how to add drag and drop to a has_many through association? I have a Model for Boards and each Board has many Lists. Each List has many Cards and each Card has many Lists through a join model called ListCard I am trying…
4
votes
1 answer

How to save my many-to-many relationship?

I am trying to create a form to save quite a complexish relationship. The models for the relationship are below. class Goal < ActiveRecord::Base belongs_to :goal_status belongs_to :goal_type has_many :users, through: :user_goals has_many…
4
votes
5 answers

Creating has_many :through records 2x times

I have models class Question < ActiveRecord::Base WEIGHTS = %w(medium hard easy) belongs_to :test has_many :answers, :dependent => :destroy has_many :testing_questions end class Testing < ActiveRecord::Base belongs_to :student,…
Antiarchitect
  • 1,852
  • 2
  • 16
  • 19
4
votes
3 answers

MySQL: 4 Table "has-many-through" Join?

Let's say I have the following 4 tables (for examples' sake): Owners, Trucks, Boxes, Apples. An owner can have many trucks, a truck can have many boxes and a box can have many apples. Owners have an id. Trucks have an id and owner_id. Boxes have…
nebs
  • 4,939
  • 9
  • 41
  • 70
4
votes
0 answers

Rails form with a subset of has_many :through checkboxes

I have a fairly straightforward application for assigning Abilities to Users, using Permissions as a join table. I've included the model definitions below. However, Abilities also belong to a Client. The form for adding Abilities to a User contains…
mbaird
  • 4,059
  • 2
  • 16
  • 14
4
votes
1 answer

rails nested has_many with foreign scope join misses to join table: sql error

A self running template with all models to test by yourself is available in this github gist - run it and it triggers the error. To visualize it, the structure looks like this: Colors |n| |:| |1| ----- Houses n:n Conditions |n| ----- |:| …
4
votes
2 answers

Filtering join table in has_many :through in RoR

I have the following models in which I join the Language and Products table via the Translation table using the Rails has_many :through paradigm: class Language < ActiveRecord::Base has_many :translations has_many :products, :through =>…
titaniumdecoy
  • 18,900
  • 17
  • 96
  • 133
4
votes
1 answer

Using orderable in rails_admin for a has_many :through relationship in rails 4

I'm trying figure out how to use ordering with position as given in the example at https://github.com/sferik/rails_admin/wiki/Has-many-%3Athrough-association, but without using protected attributes, and instead using Rails 4's strong parameters. If…
4
votes
3 answers

Multiple scoped has_many through relationship in Rails 4

I want to model such a relationship between the models User and Event. Therefore I have started with the following classes: class User < ActiveRecord::Base ... end class Attendance < ActiveRecord::Base # with columns user_id and…
contradictioned
  • 1,253
  • 2
  • 14
  • 26
4
votes
2 answers

Using HABTM or Has_many through with Active Admin

I've read quite a few of the posts on using active admin with has_many through association but I'm not getting the desired results. Essentially I have 2 models "Conferences" & "Accounts". I need to assign multiple accounts to a conference and…
DaveG
  • 1,203
  • 1
  • 25
  • 45