Questions tagged [associations]

Associations typically refer to relationships between models in ORMs such as ActiveRecord.

Associations typically refer to relationships between models in ORMs such as ActiveRecord.

Typically associations are classified based on the number of associated models:

  • one to many: one of the models stores the ID of the other
  • one to one: models (which 'belong to') each store the ID of the one associated model (which 'has many')
  • many to many: the models are linked via a join table in the database which may (called sometimes 'has many through') or may not (called 'has and belongs to many') be a model itself
6131 questions
64
votes
4 answers

Ruby-on-Rails: Multiple has_many :through possible?

Is it possible to have multiple has_many :through relationships that pass through each other in Rails? I received the suggestion to do so as a solution for another question I posted, but have been unable to get it to work. Friends are a cyclic…
William Jones
  • 18,089
  • 17
  • 63
  • 98
63
votes
8 answers

Validation failed Class must exist

I have been (hours) trouble with associations in Rails. I found a lot of similar problems, but I couldn't apply for my case: City's class: class City < ApplicationRecord has_many :users end User's class: class User < ApplicationRecord …
57
votes
4 answers

rails override default getter for a relationship (belongs_to)

So I know how to override the default getters for attributes of an ActiveRecord object using def custom_getter return self[:custom_getter] || some_default_value end I'm trying to achieve the same thing however for a belongs to association. For…
brad
  • 31,987
  • 28
  • 102
  • 155
56
votes
7 answers

How to use ActiveAdmin on models using has_many through association?

I am using ActiveAdmin gem in my project. I have 2 models using has_many through association. The database schema looks exactly the same as the example in RailsGuide.…
54
votes
3 answers

Rails 4 find or create by method doesn't work

I have a one to many association between jobs and companies and it works fine. In the job form view I have text_field for the company name with an autocomplete feature. The autocomplete works fine but the find_or_create_by don't create a new company…
Loenvpy
  • 899
  • 1
  • 10
  • 30
51
votes
8 answers

UML aggregation vs association

From Martin Fowler's UML Distilled: In the pre-UML days, people were usually rather vague on what was aggregation and what was association. Whether vague or not, they were always inconsistent with everyone else. As a result, many modelers think…
Andna
  • 6,539
  • 13
  • 71
  • 120
51
votes
9 answers

Sails.js populate nested associations

I've got myself a question regarding associations in Sails.js version 0.10-rc5. I've been building an app in which multiple models are associated to one another, and I've arrived at a point where I need to get to nest associations somehow. There's…
Lars Dol
  • 765
  • 1
  • 6
  • 14
50
votes
4 answers

How do I prevent deletion of parent if it has child records?

I have looked through the Ruby on Rails guides and I can't seem to figure out how to prevent someone from deleting a Parent record if it has Children. For example. If my database has CUSTOMERS and each customer can have multiple ORDERS, I want to…
Rob
  • 792
  • 2
  • 7
  • 15
50
votes
3 answers

When will ActiveRecord save associations?

I know that it will save associations when autosave: true as per https://api.rubyonrails.org/classes/ActiveRecord/AutosaveAssociation.html I know that it will save associations that are constructed like book = Book.new(name:…
hrdwdmrbl
  • 4,814
  • 2
  • 32
  • 41
50
votes
4 answers

Correct way of testing "associations" with Rspec?

I am trying to test the following scenario: -> I have a model called Team which it just makes sense when it has been created by a User. Therefore, each Team instance has to be related to a User. In order to test that, I have done the…
Hommer Smith
  • 26,772
  • 56
  • 167
  • 296
47
votes
3 answers

How to build many-to-many relations using SQLAlchemy: a good example

I have read the SQLAlchemy documentation and tutorial about building many-to-many relation but I could not figure out how to do it properly when the association table contains more than the 2 foreign keys. I have a table of items and every item has…
duduklein
  • 10,014
  • 11
  • 44
  • 55
46
votes
2 answers

build method on ruby on rails

New to rails and I'm following the Depot project found in the Agile web development with rails 3.1. Everything was fine until I got lost when the book used the "build" method. @cart = current_cart product =…
Finks
  • 1,661
  • 2
  • 16
  • 25
45
votes
2 answers

Rails Associations - has_many => :through - but same model

What I am trying to do: I have a blog and want to show related posts below the main post. class Post < ActiveRecord::Base has_many :related_posts has_many :posts, :through => :related_posts end And then in the join model/table class…
thenengah
  • 42,557
  • 33
  • 113
  • 157
43
votes
2 answers

Same Model for Two belongs_to Associations

I have an model PointOfContact which has_many Systems. From the Systems side I want to identify the PointOfContact as either the technical_manager or project_manager (or both). While still only keeping the PointOfContact 1 time in the DB. My attempt…
Ryan
  • 6,432
  • 7
  • 40
  • 54
42
votes
8 answers

Rails idiom to avoid duplicates in has_many :through

I have a standard many-to-many relationship between users and roles in my Rails app: class User < ActiveRecord::Base has_many :user_roles has_many :roles, :through => :user_roles end I want to make sure that a user can only be assigned any role…
KingPong
  • 1,439
  • 1
  • 16
  • 22