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

Rails Model, belongs to many

I'm having a hard time figuring out how to association one of my models with multiple of another. As it is now, I have: class ModelA < ActiveRecord::Base has_many :model_b end class ModelB < ActiveRecord::Base belongs_to…
ardavis
  • 9,842
  • 12
  • 58
  • 112
29
votes
4 answers

Rails - check if record exists in has_many association

I'm not sure if my question is worded correctly. I have three models: User, Item, and UserItem. user has_many :user_items user has_many :items, through :user_items item has_many :user_items item has_many :users -> {uniq}, through :user_items item…
user4584963
  • 2,403
  • 7
  • 30
  • 62
29
votes
4 answers

belongs_to with :class_name option fails

I have no idea what went wrong but I can't get belongs_to work with :class_name option. Could somebody enlighten me. Thanks a lot! Here is a snip from my code. class CreateUsers < ActiveRecord::Migration def self.up create_table :users…
crackpot
  • 291
  • 1
  • 3
  • 3
29
votes
3 answers

FactoryGirl override attribute of associated object

This is probably silly simple but I can't find an example anywhere. I have two factories: FactoryGirl.define do factory :profile do user title "director" bio "I am very good at things" linked_in "http://my.linkedin.profile.com" …
bobomoreno
  • 2,848
  • 5
  • 23
  • 42
29
votes
4 answers

Getting fields_for and accepts_nested_attributes_for to work with a belongs_to relationship

I cannot seem to get a nested form to generate in a rails view for a belongs_to relationship using the new accepts_nested_attributes_for facility of Rails 2.3. I did check out many of the resources available and it looks like my code should be…
Billy Gray
  • 1,747
  • 4
  • 18
  • 23
28
votes
5 answers

ActiveRecord: How can I clone nested associations?

I'm currently cloning a single-level association like this: class Survey < ActiveRecord::Base def duplicate new_template = self.clone new_template.questions << self.questions.collect { |question| question.clone } new_template.save …
Shpigford
  • 24,748
  • 58
  • 163
  • 252
28
votes
2 answers

Pluck associated model's attribute in Rails query

In my rails app, collections have many projects, and projects have many steps. I'd like to grab all the ids of steps in a collection's projects, and I'm wondering if I can do it all in one query. For example, I know I can do the following step_ids =…
scientiffic
  • 9,045
  • 18
  • 76
  • 149
27
votes
3 answers

UML: how to implement Association class in Java

I have this UML Association class. Note that: horizontal line is a solid line and the vertical line is a dashed line. --------- --------- | |*(a) *(b)| | | CLASS |________________| CLASS | |STUDENT | …
hqt
  • 29,632
  • 51
  • 171
  • 250
26
votes
1 answer

What inverse_of does mean in mongoid?

What inverse_of does mean in mongoid associations? What I can get by using it instead of just association without it?
freemanoid
  • 14,592
  • 6
  • 54
  • 77
25
votes
4 answers

How to configure many to many relationship using entity framework fluent API

I'm trying to set up a many to many relationship in EF code first but the default conventions is getting it wrong. The following classes describes the relationship: class Product { public int Id { get; set; } public string Name { get; set;…
Fixer
  • 5,985
  • 8
  • 40
  • 58
25
votes
4 answers

Associations in Sequelize migrations

My app currently uses the Sequelize sync() method to create the database, and I want to change it to use the migrations system. One of my model has belongsTo() associations with other models, and I don't really know how to make the initial migration…
loics2
  • 616
  • 1
  • 10
  • 24
25
votes
1 answer

has_many :through with a foreign key?

I've read multiple questions about this, but have yet to find an answer that works for my situation. I have 3 models: Apps, AppsGenres and Genres Here are the pertinent fields from each of…
Shpigford
  • 24,748
  • 58
  • 163
  • 252
24
votes
2 answers

Rails query join association table with alias

I have a model Edge that belongs to the other model Node twice through different foreign keys: def Edge < ActiveRecord::Base belongs_to :first, class_name: 'Node' belongs_to :second, class_name: 'Node' end And I want to perform this query…
Andrew Starostin
  • 514
  • 1
  • 4
  • 12
23
votes
8 answers

Rails: How to limit number of items in has_many association (from Parent)

I would like to limit the number of items in an association. I want to ensure the User doesn't have more than X Things. This question was asked before and the solution had the logic in the child: The offered solution (for similar issue): class…
Matt Scilipoti
  • 1,091
  • 2
  • 11
  • 15
23
votes
2 answers

Is it recommended to make associations to enum classes in UML class diagrams?

I am designing a class diagram and I got a doubt: I have a class which have several attributes referring to Java enums and other classes that will be mapped as DB catalogs. For example, there is a class called BankAccount which has an attribute…
htafoya
  • 18,261
  • 11
  • 80
  • 104