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

Can has_one association be used when the model has one or zero instances of another model?

RailsGuides says: http://guides.rubyonrails.org/association_basics.html A has_many "association indicates that each instance of the model has zero or more instances of another model." "A has_one association also sets up a one-to-one connection with…
user2725109
  • 2,286
  • 4
  • 27
  • 46
12
votes
4 answers

ExtJS 4.1 - Returning Associated Data in Model.Save() Response

I am curious as to why the record contained in the result set of a Model.save() response does not properly return updated associated data, despite the updated data being contained in the server response... Example Model & Store…
John Hall
  • 1,346
  • 13
  • 27
12
votes
2 answers

First_or_create with update on match?

I really like the first_or_create method: # Find the first user named Scarlett or create a new one with a particular last name. User.where(:first_name => 'Scarlett').first_or_create(:last_name => 'Johansson') # =>
Ben G
  • 26,091
  • 34
  • 103
  • 170
11
votes
2 answers

Implementing ActiveRecord-like associations for an API wrapper

I recently wrote ParseResource, which is a Ruby API wrapper for Parse.com's REST api. Here's a some basic usage: class Post < ParseResource fields :title, :author, :body end p = Post.create(:title => "Hello world", :author => "Alan", :body =>…
user94154
  • 16,176
  • 20
  • 77
  • 116
11
votes
3 answers

What is the easiest way to implement terms association mining in Solr?

Association mining seems to give good results for retrieving related terms in text corpora. There are several works on this topic including well-known LSA method. The most straightforward way to mine associations is to build co-occurrence matrix of…
ffriend
  • 27,562
  • 13
  • 91
  • 132
11
votes
1 answer

Making a Rails relationship with a custom name

I have a model called company and one called user, and User belongs to Company and Company has many Users. But I want to store on the company model the master company admin user, but I want to do it with a custom name. So, i want to do this:…
Mateus Pinheiro
  • 870
  • 2
  • 12
  • 20
11
votes
5 answers

Rails nested attributes: require at least two records

How can I make it so that at least two option records are required to submit a product? class Product < ActiveRecord::Base belongs_to :user has_many :options, :dependent => :destroy accepts_nested_attributes_for :options, :allow_destroy =>…
morcutt
  • 3,739
  • 8
  • 30
  • 47
11
votes
1 answer

Rails 3 build a select tag with has_many belongs_to association

Based on following models class Company < ActiveRecord::Base belongs_to :country end class Country < ActiveRecord::Base has_many :companies end I want to have in my companies/_form a select tag containing all the countries I think that the…
denisjacquemin
  • 7,414
  • 10
  • 55
  • 72
11
votes
1 answer

Overriding default identifier generation strategy has no effect on associations

Symfony 2.7.2. Doctrine ORM 2.4.7. MySQL 5.6.12. PHP 5.5.0. I have an entity with custom ID generator strategy. It works flawlessly. In some circumstances I have to override this strategy with a "handmade" Id. It works when the main entity is being…
Taz
  • 3,718
  • 2
  • 37
  • 59
11
votes
3 answers

Does ActiveRecord save a belongs_to association when saving main object?

If I have two models: class Post < ActiveRecord::Base belongs_to :user end and class User < ActiveRecord::Base has_many :posts end If I do: post = Post.new user = User.new post.user = user post.save Does the user get saved as well and the…
agentofuser
  • 8,987
  • 11
  • 54
  • 85
11
votes
2 answers

How to use the Devise gem in a Rails app where the "User" is split up between three models?

Say I have the following in a Rails 4 app: class Person < ActiveRecord::Base has_many :email_addresses, as: :emailable has_one :user_account end class EmailAddress < ActiveRecord::Base belongs_to :emailable, polymorphic: true # There is an…
robertwbradford
  • 6,181
  • 9
  • 36
  • 61
11
votes
2 answers

Rails has_many through aliasing with source and source_type for multiple types

So here is a sample class class Company < ActiveRecord::Base has_many :investments has_many :vc_firms, through: :investments, source: :investor, source_type: 'VentureFirm' has_many :angels, through: :investments, source: :investor,…
11
votes
1 answer

Can a model "belongs_to" either/or more than one model?

Apologies if this is a slightly noob question, but looking to clarify my thoughts on this. I have a model that can EITHER belong to one model, or another. For example: Let's say I have a Team model and I have a Member model, and both of those models…
aaronrussell
  • 9,389
  • 5
  • 38
  • 62
11
votes
10 answers

Get first association from a has many through association

I'm trying to join the first song of each playlist to an array of playlists and am having a pretty tough time finding an efficient solution. I have the following models: class Playlist < ActiveRecord::Base belongs_to :user has_many…
mrabin
  • 662
  • 10
  • 19
11
votes
3 answers

Factory Girl create association with existing object

I am new to FactoryGirl and I am trying the following simple scenario? factory :female, :class => Gender do code 'Female' end factory :male, :class => Gender do code 'Male' end factory :papas, :class => Customer do first_name 'Jim' …
p.matsinopoulos
  • 7,655
  • 6
  • 44
  • 92