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

Rails 4: How to use includes() with where() to retrieve associated objects

I can't figure out how to user the .where() method to retrieve associated model data. In this example, Projects belongs_to Users... class Project < ActiveRecord::Base belongs_to :user has_many :videos end class User < ActiveRecord::Base …
emersonthis
  • 32,822
  • 59
  • 210
  • 375
22
votes
2 answers

How to rectify versions on has_many/belongs_to association with paper_trail

I use paper_trail in rails to track my models versions. But the documentation on the github repo indicates that the gem doesn't support has_many, belongs_to associations. Let's say I've an app that records the ceos names of some comapnies: class…
blawzoo
  • 2,465
  • 4
  • 19
  • 24
22
votes
1 answer

Rails belongs_to association (with :class_name) returns nil

I'm relatively new to Rails development and I'm having a minor associations problem. I'd like to name an association something different than the model it's linked to. I have the following 2 models: class User < ActiveRecord::Base has_many…
Graham Swan
  • 4,818
  • 2
  • 30
  • 39
22
votes
2 answers

Reassociate files types for visual studio 2012

I've had an install of 2012 for a while, but I've had to also just install VS 2010. As expected, all my file associations now point to VS2010 and the icons aren't so intuitive. Without doing each file manually through the explorer menu, how can I…
friartuck
  • 2,954
  • 4
  • 33
  • 67
22
votes
5 answers

Rails Associations has_one Latest Record

I have the following model: class Section < ActiveRecord::Base belongs_to :page has_many :revisions, :class_name => 'SectionRevision', :foreign_key => 'section_id' has_many :references has_many :revisions, :class_name => 'SectionRevision',…
Ryan King
  • 3,538
  • 12
  • 48
  • 72
21
votes
3 answers

What is the "rails way" to enforce a has_many but has-only-one-current association?

I have a simple rails app with models project and phase. A project has many phases, but only on phase can be active (i.e. "current") at a time. I still want the other phases to be accessible, but the current phase should be the main anchor for the…
emrass
  • 6,253
  • 3
  • 35
  • 57
21
votes
1 answer

Using epilogue, is it possible to get back a resource without associations?

I have epilogue.resource({ model: db.Question, endpoints: ['/api/questions', '/api/questions/:id'], associations: true }); So when I hit /api/questions, I get back all the associations with the resources. Is there something I can pass to not…
Shamoon
  • 41,293
  • 91
  • 306
  • 570
20
votes
4 answers

Is it possible to get the ActiveRecord::Relation object for an association

Do association methods, such as those defined by has_many and belongs_to utilize ActiveRecord::Relation? If so, is it possible to get the ActiveRecord::Relation object that is being used. We're all aware that Rails 3 is heavily using…
John
  • 9,254
  • 12
  • 54
  • 75
20
votes
2 answers

Ruby on Rails - Association gets deleted before "before_destroy"

I have an object A that has_many B's (simple association): has_many :book_accounts, { dependent: :destroy } I was working on a before_destroy callback. I want to check and make sure that there are no C's (which belongs_to B) and D's (which…
Isaac
  • 2,246
  • 5
  • 21
  • 34
19
votes
1 answer

How can I delete child objects when the parent is deleted in rails?

model a: has_many :b, :dependent => :delete_all model b: belongs_to :a belongs_to :c model c: has_many :b When I delete an a, I would also like to have children b's deleted so that they get removed from any c's that may reference them. However,…
James
  • 5,273
  • 10
  • 51
  • 76
19
votes
2 answers

Rails has_one vs belongs_to semantics

I have a model representing a Content item that contains some images. The number of images are fixed as these image references are very specific to the content. For example, the Content model refers to the Image model twice (profile image, and…
Anurag
  • 140,337
  • 36
  • 221
  • 257
19
votes
2 answers

Rails association for two foreign keys for the same table in one table

I am new to RoR and still playing with associations. I need to have two references to a particular model in another model. The scaffolded code doesn't work and I get a "uninitialized constant" error. Generation commands: script/generate scaffold…
Rohit
  • 1,710
  • 5
  • 20
  • 29
19
votes
2 answers

rails renaming associations

I have two models, TreeNode and User. Each user has_one TreeNode, which is the root of the tree. class TreeNode acts_as_tree belongs_to :user end class User has_one :tree_node end I would like to have this setup so that rails will make the…
Bryan Ward
  • 6,443
  • 8
  • 37
  • 48
18
votes
4 answers

Is there any way to check that has_many association exists in Rails 3.1?

For example there are some models class Model_1 < ActiveRecord::Base has_many :images, :as => :imageable end class Model_2 < ActiveRecord::Base # doesn't have has_many association end ... class Image < ActiveRecord::Base belongs_to…
Babur Ussenakunov
  • 1,995
  • 2
  • 16
  • 16
18
votes
6 answers

Rails - Parent/child relationships

I'm currently using a standard one-to-one relationship to handle parent/child relationships: class Category < ActiveRecord::Base has_one :category belongs_to :category end Is there a recommended way to do it or is this ok?
bcoughlan
  • 25,987
  • 18
  • 90
  • 141