Questions tagged [model-associations]

Models can associate in three main ways: one to many, one to one, and many to many.

This is tagged primarily in Ruby community. Models can associate in three main ways: one to many, one to one, and many to many.

865 questions
94
votes
8 answers

Rails association with multiple foreign keys

I want to be able to use two columns on one table to define a relationship. So using a task app as an example. Attempt 1: class User < ActiveRecord::Base has_many :tasks end class Task < ActiveRecord::Base belongs_to :owner, class_name:…
63
votes
8 answers

Validation failed Class must exist

I have been (hours) trouble with associations in Rails. I found a lot of similar problems, but I couldn't apply for my case: City's class: class City < ApplicationRecord has_many :users end User's class: class User < ApplicationRecord …
60
votes
4 answers

Aggregation vs Composition vs Association vs Direct Association

I am reviewing my knowledge in object-oriented programming. Under the relationship between classes topic, I have encountered some relationships which are a bit ambiguous to me. I know dependency "uses-a" and inheritance "is-a" but I'm a bit…
steven0529
  • 1,513
  • 1
  • 18
  • 28
43
votes
2 answers

Same Model for Two belongs_to Associations

I have an model PointOfContact which has_many Systems. From the Systems side I want to identify the PointOfContact as either the technical_manager or project_manager (or both). While still only keeping the PointOfContact 1 time in the DB. My attempt…
Ryan
  • 6,432
  • 7
  • 40
  • 54
43
votes
1 answer

Flask/SQLAlchemy - Difference between association model and association table for many-to-many relationship?

I started learning this stuff from the Flask Mega Tutorial. When he gets into Many-to-Many relationships, he creates an association table like this: followers = db.Table('followers', db.Column('follower_id', db.Integer,…
32
votes
4 answers

ExtJS 4: Models with Associations and Stores

Introduction I'm facing an application design problem with the Ext.data.Model class in ExtJS. I will try to develop my ideas to a very common online store scenario here, so you can follow me. I would really appreciate any comments on my thoughts and…
Konrad Kleine
  • 4,275
  • 3
  • 27
  • 35
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
20
votes
4 answers

How do rails association methods work?

How do rails association methods work? Lets consider this example class User < ActiveRecord::Base has_many :articles end class Article < ActiveRecord::Base belongs_to :user end Now I can do something like @user =…
Chirantan
  • 15,304
  • 8
  • 49
  • 75
16
votes
3 answers

Sunspot rails: include associated models when calling .results

Let's say that I want to search for Events in my app. When I display the results, I want to display who created the event, for instance. Is there any way to add .includes(:user) somewhere, to avoid unnecessary queries (one for each event)? I can't…
Robin
  • 21,667
  • 10
  • 62
  • 85
16
votes
1 answer

Rails associations in namespaced models

I'm re-doing an app and migrating data from an old app. The some of the models names will be the same, though not all of them. I'm writing a rake task to connect to the old database, read the records, do some stuff and write the result into a new…
John Small
  • 942
  • 2
  • 12
  • 21
14
votes
1 answer

Rails - execution sequence of after create callback & nested attributes

I have a simple set up of User and UserProfile model with User has_one :user_profile and UserProfile belongs_to :user. But I am unable to wrap my head around how Rails defines execution order of after_create callback and…
prasvin
  • 3,009
  • 23
  • 28
12
votes
4 answers

Access Nested Backbone Model Attributes from Mustache Template

I have one Backbone model which has an attribute that is a reference to another Backbone model. For example, a Person has a reference to an Address object. Person FirstName LastName Address Street City State Zip These are…
Kevin
  • 2,084
  • 3
  • 24
  • 36
12
votes
1 answer

Rails 4: Difference between validates presence on id or association

If I have a 'belongs_to' association in a model, I'd like to know the notional difference between validating an association: class Topping < ActiveRecord::Base belongs_to :pancake validates :pancake, presence: true ... and…
12
votes
0 answers

How do I show (collection) associations in Visual Studio's generated class diagrams?

Visual studio doesn't seem to recognize c++ STL classes properly when it comes to generating class diagrams. There are two options when right-clicking class elements in the generated diagrams: - Show as Association - Show as Collection…
11
votes
6 answers

extjs 4 XTemplate class associations

I'm testing out extjs 4 and I have stumbled upon something, I can't seem to figure out. I have simple object association: Snapshot - hasMany -> Model Now, I am trying to use XTemplate to show this association in View component, so I have my…
Greg
  • 2,413
  • 5
  • 22
  • 23
1
2 3
57 58