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
15
votes
7 answers

Linq To SQL Without Explicit Foreign Key Relationships

I am working with a few legacy tables that have relationships, but those relationships haven't been explicitly set as primary/foreign keys. I created a .dbml file using "Linq To Sql Classes" and established the proper Case.CaseID = CaseInfo.CaseID…
madcolor
  • 8,105
  • 11
  • 51
  • 74
15
votes
1 answer

How to use Sequelize belongsToMany associations?

I have projects and users. A user can have many projects. A project can have multiple users. I tried to model this with a belongsToMany association. On my server I defined the associations like this: user.belongsToMany(project, { through:…
K..
  • 4,044
  • 6
  • 40
  • 85
15
votes
6 answers

Grails - Removing an item from a hasMany association List on data bind?

Grails offers the ability to automatically create and bind domain objects to a hasMany List, as described in the grails user guide. So, for example, if my domain object "Author" has a List of many "Book" objects, I could create and bind these using…
ecrane
  • 191
  • 1
  • 1
  • 4
15
votes
2 answers

Could Not Find Inverse Association for has_many in Rails 3

I have the following models: class Business < ActiveRecord::Base has_many :customers, :inverse_of => :business has_many :payments, :inverse_of => :business end class Customer < ActiveRecord::Base belongs_to :business, :inverse_of =>…
darksky
  • 20,411
  • 61
  • 165
  • 254
15
votes
5 answers

Validate presence of polymorphic parent

I am developing a Rails 3.2 application with the following models: class User < ActiveRecord::Base # Associations belongs_to :authenticatable, polymorphic: true # Validations validates :authenticatable, presence: true # this is the critical…
Dennis Hackethal
  • 13,662
  • 12
  • 66
  • 115
14
votes
2 answers

rails mongoid criteria find by association

I'm trying to find a record by associated username which is included in a belongs_to relation, but it's not working. Articles belong to Users Users have many articles Article.where(user_id: someid) works fine, but I'd like to use the username as…
ere
  • 1,739
  • 3
  • 19
  • 41
14
votes
2 answers

Rails: ActiveRecord::HasManyThroughSourceAssociationNotFoundError: Could not find the source association(s)

I have the following code (somewhat simplified ... create_table :signatures do |t| t.integer :signer_id t.integer :card_id t.timestamps end With the models looking like ... class Signature < ActiveRecord::Base belongs_to :card …
slabounty
  • 704
  • 11
  • 21
14
votes
4 answers

Rails: Why "has_many ..., :through => ..." association results in "NameError: uninitialized constant ..."

To express that a group can have multiple users, and a user can belong to multiple groups, I set the following associations: class Group < ActiveRecord::Base has_many :users_groups has_many :users, :through => :users_groups end class User <…
Misha Moroshko
  • 166,356
  • 226
  • 505
  • 746
14
votes
2 answers

What methods/mixins sequelize adds to the models when an association is made?

Edit: Some time after I wrote this Q&A, I've made improvements to the Sequelize documentation itself, regarding multiple topics, including this one. For those interested, the original Q&A is kept below, but I recommend just reading the new…
Pedro A
  • 3,989
  • 3
  • 32
  • 56
14
votes
6 answers

Rails Polymorphic has_many

Using Ruby on Rails, how can I achieve a polymorphic has_many relationship where the owner is always of a known but the items in the association will be of some polymorphic (but homogenous) type, specified by a column in the owner? For example,…
maerics
  • 151,642
  • 46
  • 269
  • 291
14
votes
4 answers

Rails - Testing fixtures error NoMethodError: undefined method `type' for nil:NilClass

I have a problem running tests that use fixtures with associations between models. Here's the error I get, as soon as I run rake test: ERROR["test_truth", SevenPortfolioTest, 0.005154775] test_truth#SevenPortfolioTest (0.01s) NoMethodError: …
Rafael Adel
  • 7,673
  • 25
  • 77
  • 118
14
votes
3 answers

Rails 3: validate presence of at least one has many through association item

I have two models: Project and ProjectDiscipline: class Project < ActiveRecord::Base has_many :project_disciplinizations, :dependent => :destroy has_many :project_disciplines, through: :project_disciplinizations attr_accessible…
leonel
  • 10,106
  • 21
  • 85
  • 129
14
votes
1 answer

Rails Merge child errors with Parent errors

I've got the following two (sanitized/stylized) models: class DrivingExam < ActiveRecord::Base belongs_to :dmv_rules has_many :invigilator_assignments, as: :assignable has_many :invigilator, through: :invigilator_assignments validate…
Abraham P
  • 15,029
  • 13
  • 58
  • 126
14
votes
2 answers

bi-directional and uni-directional associations UML

While I think I understand aggregation and composition, I am having difficulties understanding bi-directional and uni-directional association. I've read that with bi-directional association, both classes know about each other and with…
jcxz
  • 1,226
  • 2
  • 12
  • 25
14
votes
2 answers

Why would I NOT want to use inverse_of everywhere?

As outlined here: http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html inverse_of appears to tell Rails to Cache the in memory associations and minimize Database Queries. Their example is: class Dungeon <…
Abraham P
  • 15,029
  • 13
  • 58
  • 126