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
5883 questions
436
votes
20 answers

What is the difference between association, aggregation and composition?

What is the difference between association, aggregation, and composition? Please explain in terms of implementation.
None
204
votes
6 answers

What is causing this ActiveRecord::ReadOnlyRecord error?

This follows this prior question, which was answered. I actually discovered I could remove a join from that query, so now the working query is start_cards = DeckCard.find :all, :joins => [:card], :conditions => ["deck_cards.deck_id = ? and…
user26270
  • 6,194
  • 13
  • 53
  • 82
197
votes
4 answers

Rails :dependent => :destroy VS :dependent => :delete_all

In rails guides it's described like this: Objects will be in addition destroyed if they’re associated with :dependent => :destroy, and deleted if they’re associated with :dependent => :delete_all Right, cool. But what's the difference between…
Sergey
  • 4,464
  • 6
  • 22
  • 30
160
votes
4 answers

What is the difference between Unidirectional and Bidirectional JPA and Hibernate associations?

What is the difference between Unidirectional and Bidirectional associations? Since the table generated in the db are all the same,so the only difference I found is that each side of the bidiretional assocations will have a refer to the other,and…
hguser
  • 31,173
  • 49
  • 145
  • 269
150
votes
5 answers

MongoDB Many-to-Many Association

How would you do a many-to-many association with MongoDB? For example; let's say you have a Users table and a Roles table. Users have many roles, and roles have many users. In SQL land you would create a UserRoles table. Users: Id …
Josh Close
  • 20,425
  • 11
  • 83
  • 131
148
votes
4 answers

Rails: Using build with a has_one association in rails

In this example, I create a user with no profile, then later on create a profile for that user. I tried using build with a has_one association but that blew up. The only way I see this working is using has_many. The user is supposed to only have at…
espinet
  • 1,643
  • 2
  • 11
  • 7
134
votes
5 answers

Rails migration: t.references with alternative name?

So I have a create_table like this for Courses at a School: create_table :courses do |t| t.string :name t.references :course t.timestamps end but I want it to reference two other courses like: has_many :transferrable_as # A Course has_many…
themirror
  • 8,937
  • 6
  • 38
  • 71
126
votes
5 answers

Build vs new in Rails 3

In the Rails 3 docs, the build method for associations is described as being the same as the new method, but with the automatic assignment of the foreign key. Straight from the docs: Firm#clients.build (similar to Client.new("firm_id" => id)) I've…
ClosureCowboy
  • 18,648
  • 13
  • 53
  • 68
124
votes
11 answers

How to create has_and_belongs_to_many associations in Factory girl

Given the following class User < ActiveRecord::Base has_and_belongs_to_many :companies end class Company < ActiveRecord::Base has_and_belongs_to_many :users end how do you define factories for companies and users including the bidirectional…
opsb
  • 26,793
  • 17
  • 85
  • 96
85
votes
7 answers

Rails association with multiple foreign keys

I want to be able to use two columns on one table to define a relationship. So using a task app as an example. Attempt 1: class User < ActiveRecord::Base has_many :tasks end class Task < ActiveRecord::Base belongs_to :owner, class_name:…
75
votes
7 answers

Rails - Best-Practice: How to create dependent has_one relations

What's the best practice to create has_one relations? For example, if I have a user model, and it must have a profile... How could I accomplish that? One solution would be: # user.rb class User << ActiveRecord::Base after_create…
69
votes
3 answers

Rails has_one :through association

Rails has a has_one :through association that helps set up a one-to-one association with a third model by going through a second model. What is the real use of that besides making a shortcut association, that would otherwise be an extra step…
Anurag
  • 132,806
  • 34
  • 214
  • 257
64
votes
9 answers

What is the best way to implement Polymorphic Association in SQL Server?

I have tons of instances where I need to implement some sort of Polymorphic Association in my database. I always waste tons of time thinking through all the options all over again. Here are the 3 I can think of. I'm hoping there is a best practice…
64
votes
4 answers

Rails: ActiveRecord query based on association value

I have 2 models. Report and Server that have a belongs_to and has_many relationship. I created an accessor method using delegate that allows a Report to find its associated Server.company_id. Now, I want to run a query on Report that allows me to…
user2158382
  • 4,100
  • 11
  • 47
  • 94
58
votes
4 answers

add associations to exisiting models

I'm wondering how I can add associations to my models. Suppose, I generate two models rails generate model User rails generate model Car Now I want to add an associations so that the models acquire the form class User < ActiveRecord::Base …
Andrew
  • 1,958
  • 5
  • 19
  • 33
1
2 3
99 100