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
18
votes
2 answers

When to use association, aggregation, composition and inheritance?

I've seen plenty of posts on Stackoverflow explaining the difference between the relationships: associations, aggregation, composition and inheritance, with examples. However, I'm more specifically confused more about the pros and cons of each of…
18
votes
1 answer

ActiveRecord::HasManyThroughAssociationNotFoundError in UserController#welcome

I have a many to many relationship in rails. All database tables are named accordingly and appropriately. All model files are plural and use underscore to seperate words. All naming comventions are followed by ruby and rails standards. I'm using…
18
votes
3 answers

Rails belongs_to_many

I'm a beginner in Rails and I have a problem with ActiveRecords associations. I'm creating simple car rental service and I made the following associations: class Client < ActiveRecord::Base has_many :rentals has_many :bookings has_many :cars,…
Kamil Lelonek
  • 14,592
  • 14
  • 66
  • 90
18
votes
2 answers

Associating existing unassociated changesets with workitem in TFS

I know there're powerful TFS command line tools from TFS Power tools that provide much more that can be achieved through Visual Studio integrated capabilities. I have several changesets that aren't associated with any workitem. I would like to…
Robert Koritnik
  • 103,639
  • 52
  • 277
  • 404
17
votes
3 answers

UML association multiplicity

I have a question about association multiplicity. I understand it, but for example if I would have: --------- --------- | |1 * | | |CLASS |----------| STUDENT | | | | | --------- …
Andna
  • 6,539
  • 13
  • 71
  • 120
17
votes
4 answers

how to preselect an association checkbox using simple_form

I have this piece of code, while using simple_form: = simple_form_for :report do |f| = f.association :presets, :collection => @account.presets.collect{ |p| [p.name, p.id] }, :as => :check_boxes How can I preselect a specific preset…
Michał
  • 374
  • 1
  • 3
  • 16
17
votes
2 answers

Eager load associations with Active Model Serializers

Background I have a rails application with deeply nested associations. .-< WorkPeriod Timecard -< Week -< Day -<--< Subtotal `-< Adjustment -< (has many) I'm using Active Model Serializer to…
16
votes
5 answers

How can I define a polymorphic relation between models in Django?

I am working on a Django application which contains an Offer model. An Offer instance contains the pricing conditions and points to a product definition. The product model is actually a hierarchy (I have a Television model, a Camcorder model,…
MiniQuark
  • 46,633
  • 36
  • 147
  • 183
16
votes
3 answers

Rails 4 Migration | Add Table with Reference

I am attempting to create a Collaboration table in my Rails 4 project, but I've run into an issue. I wish it to belong_to a single user, the collaborator. I ran the following command to generate the model and the migration, which I've also copied…
16
votes
2 answers

How to filter by conditions for associated models?

I have a belongsToMany association on Users and Contacts. I would like to find the Contacts of the given User. I would need something like $this->Contacts->find()->contain(['Users' => ['Users.id' => 1]]); The cookbook speaks about giving conditions…
rrd
  • 1,441
  • 2
  • 15
  • 36
16
votes
1 answer

How to selectively populate waterline associations via query param in sails.js

By default, sails will populate all relationships within a model when it's corresponding API route is hit. Does anyone know if it's possible to toggle this functionality? If I'm working with a one-to-many association, I may not want to populate the…
Jason Sims
  • 1,148
  • 2
  • 10
  • 22
16
votes
5 answers

Ruby on rails: Creating a model entry with a belongs_to association

I am trying to add a new entry in my database for a model that has a belongs_to relationship. I have 2 models, Jobs and Clients. It was easy enough to find tutorial on how to set up the association between these two (using has_many and belongs_to),…
user2158382
  • 4,430
  • 12
  • 55
  • 97
16
votes
3 answers

Rails - Custom html into simple_form label

I'm trying to customize the output of a simple_form association, basically I need to display a checkbox label on two lines. My idea was adding a "br" tag into the "label", but unfortunately it gets escaped so it display actually "br" instead of…
Carlo
  • 1,184
  • 4
  • 15
  • 31
16
votes
1 answer

difference between collection and association mapping in mybatis 3

I am doing mysql queries execution from mybatis3. I am new to this. What is the difference between collection and association mapping in mybatis 3? Specific example below. SELECT e.empid AS empid,e.empname AS empname, e.empsalary AS…
Pratap M
  • 1,059
  • 3
  • 21
  • 31
16
votes
5 answers

ActiveAdmin how to sort column with associations

I'm developing an ActiveAdmin app, and I want to sort a column of businesses by their "type". Unfortunately my code is not working. What code should I use to accomplish this? Here is my code... app/models/business.rb class Business <…
Slicekick
  • 2,119
  • 5
  • 24
  • 35