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

Rails association - how to add the 'has_many' object to the 'owner'

In my app, a user has many score_cards and a score_card belongs to a user The question is, whenever I create a new score_card, ie, ScoreCardsController.create gets called, how do I add this newly created score_card to the current_user (I'm using…
ryanprayogo
  • 11,587
  • 11
  • 51
  • 66
41
votes
2 answers

How do I remove a single HABTM associated item without deleting the item itself?

How do you remove a HABTM associated item without deleting the item itself? For example, say I have 3 Students that are in a Science class together. How do I remove the Science objects from the StudentsClasses table without deleting the actual…
humble_coder
  • 2,777
  • 7
  • 34
  • 46
39
votes
2 answers

Could not find the association problem in Rails

I am fairly new to Ruby on Rails, and I clearly have an active record association problem, but I can't solve it on my own. Given the three model classes with their associations: # application_form.rb class ApplicationForm < ActiveRecord::Base …
Ash
  • 24,276
  • 34
  • 107
  • 152
39
votes
1 answer

Hibernate @OneToMany remove child from list when updating parent

I have the following entities: TEAM @Entity @Table public class Team { [..] private Set userTeamRoles; /** * @return the userTeamRoles */ @OneToMany(cascade = { CascadeType.ALL }, mappedBy = "team", fetch = FetchType.LAZY) public…
AndaP
  • 1,308
  • 8
  • 23
  • 40
36
votes
3 answers

How do I pass an argument to a has_many association scope in Rails 4?

Rails 4 lets you scope a has_many relationship like so: class Customer < ActiveRecord::Base has_many :orders, -> { where processed: true } end So anytime you do customer.orders you only get processed orders. But what if I need to make the where…
Rob Sobers
  • 20,737
  • 24
  • 82
  • 111
35
votes
4 answers

Rails: belongs_to vs has_one

A bit of a newbie question on rails associations. I have a Bug model, and a Status model. Status is basically just a key/value pair table. Out of the choices available, I would say Bug has_one Status makes the most sense. However, according to…
Matt Briggs
  • 41,224
  • 16
  • 95
  • 126
33
votes
1 answer

Is there a "first_or_build" method on has_many associations?

In rails 3.2+, you can do this : SomeModel.some_scope.first_or_initialize Which means you can also do : OtherModel.some_models.first_or_initialize I find this pretty useful, but i'd like to have a first_or_build method on my has_many…
m_x
  • 12,357
  • 7
  • 46
  • 60
33
votes
2 answers

Hibernate unidirectional one to many association - why is a join table better?

In this document (scroll down to the Unidirectional section): http://docs.jboss.org/hibernate/stable/annotations/reference/en/html_single/#entity-mapping-association-collections it says that a unidirectional one-to-many association with a join table…
UrLicht
  • 939
  • 1
  • 14
  • 25
33
votes
6 answers

rails scope to check if association does NOT exist

I am looking toward writing a scope that returns all records that do not have a particular association. foo.rb class Foo < ActiveRecord::Base has_many :bars end bar.rb class Bar < ActiveRecord::Base belongs_to :foo end I want a scope…
Julio G Medina
  • 692
  • 1
  • 7
  • 17
31
votes
1 answer

C++ : Association, Aggregation and Composition

I'm beginning to study OOAD and I'm having difficulty finding a C++ code example that'd illustrate how Association, Aggregation and Composition are implemented programmatically. (There are several posts everywhere but they relate to C# or java). I…
Talha5389
  • 738
  • 1
  • 9
  • 22
30
votes
5 answers

How to do has_many and has_one association in same model?

I need to do two associations in the same model. Where: Team has_many User Now, I want that Team has_one Leader This "Leader" will be a User Im trying to use has_one throught but I think that association isn't work. Leader.rb class Leader <…
Igor Martins
  • 2,015
  • 7
  • 36
  • 57
30
votes
4 answers

Rails console: Unable to autoload constant

I have a Customer_ratings model that allows users to leave feedback on each other. The web app is working properly, and feedback is collected, stored and displayed. I wanted to go in and delete some feedback through the rails console, but when I…
dmt2989
  • 1,610
  • 3
  • 17
  • 30
30
votes
1 answer

The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations

"The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations." I am getting this error in Entity Framework 4.4 when updating/migrating the database, but I am not trying to specify…
lintmouse
  • 5,079
  • 8
  • 38
  • 54
30
votes
6 answers

How to join unrelated entities with the JPA Criteria API

Two database tables have a foreign key relationship. They are mapped to two entities A and B by JPA, but the join columns are manually removed from the entities, so in JPA world classes A and B are not related and you cannot navigate from one to…
Filippo
  • 1,123
  • 1
  • 11
  • 28
30
votes
4 answers

Trouble with accepts_nested_attributes_for on validating foreign key

I am using Ruby on Rails v3.2.2. I would like to solve the issue related to the validation of a foreign key when using accepts_nested_attributes_for and validates_associated RoR methods. That is, I have following model classes: class Article <…
user12882
  • 4,702
  • 9
  • 39
  • 54