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
13
votes
3 answers

Need data from rails join table, has_many :through

I have 3 tables - users, things, and follows. Users can follow things through the follows table, associating a user_id with a things_id. This would mean: class User has_many :things, :through => :follows end class Thing has_many :users,…
13
votes
2 answers

hibernate order by association

I'm using Hibernate 3.2, and using criteria to build a query. I'd like to add and "order by" for a many-to-one association, but I don't see how that can be done. The Hibernate query would end up looking like this, I guess: select t1.a, t1.b, t1.c,…
Gary Kephart
  • 4,860
  • 5
  • 39
  • 52
13
votes
1 answer

Rails how to touch Active Record Object without locking?

In my photos class I have this association. belongs_to :user, :touch => true One day I got this exception. A ActiveRecord::StatementInvalid occurred in photos#update: Mysql::Error: Deadlock found when trying to get lock; try restarting…
Eric Coulthard
  • 500
  • 6
  • 20
13
votes
3 answers

Rails habtm and finding record with no association

I have 2 models: class User < ActiveRecord::Base has_and_belongs_to_many :groups end class Group < ActiveRecord::Base has_and_belongs_to_many :users end I want to make a scope (that's important - for efficiency and for ability to chain…
schiza
  • 1,900
  • 1
  • 15
  • 18
13
votes
2 answers

Entity Framework, Code First, Update "one to many" relationship with independent associations

It took me way too long to find a solution to the scenario described below. What should seemingly be a simple affair proved to be rather difficult. The question is: Using Entity Framework 4.1 (Code First approach) and "Independent associations" how…
Martin
  • 185
  • 1
  • 1
  • 10
13
votes
4 answers

UML ternary association

I'm currently having some trouble understanding ternary associations in UML. I get the binary ones, but I am unsure how multiplicity works on ternary. I'm doing exercises that I got from my university, the current one goes like this: One department…
Heeiman
  • 249
  • 1
  • 4
  • 15
13
votes
1 answer

belongs_to and has_many to the same model

I am wondering whether there is a way to do this with rails or not. Basically I have a user model and an event model. Event is created by a user and I want to have a foreign key (user_id) in the event model that indicates who created the event.…
denniss
  • 17,229
  • 26
  • 92
  • 141
13
votes
2 answers

Custom scope on has_many, :through association (Rails 4)

CONTEXT: In my setup Users have many Communities through CommunityUser, and Communities have many Posts through CommunityPost. If follows then, that Users have many Posts through Communities. User.rb has_many :community_users has_many :communities,…
neon
  • 2,811
  • 6
  • 30
  • 44
13
votes
4 answers

rails has_many :through has_many :through

I'm wondering to what extent I can use associations in Rails. Take into consideration the following: class User < ActiveRecord::Base has_one :provider has_many :businesses, :through => :provider end class Provider < ActiveRecord::Base …
bloudermilk
  • 17,820
  • 15
  • 68
  • 98
13
votes
2 answers

How can I associate a certain file type again with "View file"

After installing a fresh Ubuntu (13.04) pem files (SSL certificates) are associated with "View file" (right click the file in Nautilus I see "Open with View file"), which nicely shows a clear text version of the certificate. Now I wanted to edit…
gucki
  • 4,582
  • 7
  • 44
  • 56
13
votes
3 answers

Simple_Form Association with has_many :through extra field

I have two models, Developers and Tasks, class Developer < ActiveRecord::Base attr_accessible :address, :comment, :email, :name, :nit, :phone, :web has_many :assignments has_many :tasks, :through => :assignments end class Task <…
12
votes
1 answer

writing associations for a recursive relationship

a little help if you will. I would like create a recusive relationsip with my 'product' class. Where I can have one product be the parent product for other products. My questions is how do I create this relationship within my model? would this…
E.E.33
  • 2,013
  • 3
  • 22
  • 34
12
votes
2 answers

Rails has many and belongs to one

I have a User model which has many projects and a Project model which can have many users, but also belongs to a single user (ie the user who created this project). It must belong to a User. It also allows a list of users to be associated with it,…
Lee Jarvis
  • 16,031
  • 4
  • 38
  • 40
12
votes
1 answer

counter_cache in Rails on a scoped association

I have User model which has_many :notifications. Notification has a boolean column seen and a scope called :unseen which returns all notifications where seen is false. class User < ApplicationRecord has_many :notifications has_many…
GMA
  • 5,816
  • 6
  • 51
  • 80
12
votes
7 answers

Java many to many association map

I have two classes, ClassA and ClassB, as well as a "many to many" AssociationClass. I want a structure that holds the associations between A and B so that I can find the counterpart for each instance of A or B. I thought of using a Hashmap, with…
Raphael Jolivet
  • 3,940
  • 5
  • 36
  • 56