Questions tagged [belongs-to]

This is a master-detail relationship where one item "belongs_to" another and has a foreign key to maintain the relationship.

A master record has many detail records.
For example if we have buildings and rooms we can say that one building has_many rooms.
In a database, with tables/models, this relationship implies that rooms will have a foreign key for which building they belong to and thus the relationship will be belongs_to.

756 questions
5
votes
2 answers

In Rails, how can I retrieve the object on a belongs_to association, without going through the database?

Consider the following setup: class Parent < ActiveRecord::Base has_many :children end class Child < ActiveRecord::Base belongs_to :parent end And this console session: >> p = Parent.find 41 >> p.some_attr = 'some_value' >> c =…
KenB
  • 6,587
  • 2
  • 35
  • 31
5
votes
1 answer

belongs_to with a custom class_name not producing proper foreign key in Rails 3

I am updating an application to Rails 3 and I am having trouble creating a custom foreign key. I have something like this: class Product < ActiveRecord::Base belongs_to :owner, :class_name => 'User' ... end class User < ActiveRecord::Base …
Tony
  • 18,776
  • 31
  • 129
  • 193
5
votes
2 answers

How to create "two-side" many-to-many relationships in Rails?

Suppose we have a photography site. Any author can subscribe to receive updates from any other author. Obviously if author A is subscribed to author B that doesn't mean that B is subscribed to A. So we build models class Author < ActiveRecord::Base …
5
votes
2 answers

Saving a model with multiple foreign keys in Laravel 4

I understand that in order to save a foreign key, one should use the related model and the associate() function, but is it really worth the trouble of going through this $user = new User([ 'name' => Input::get('name'), 'email' =>…
Nicolas
  • 2,754
  • 6
  • 26
  • 41
5
votes
1 answer

Rails acts_as_paranoid - belongs_to not working with with_deleted

I have two models with a belongs_to - has_many relationship which you can see below (I included only the parts of the code relevant to this question since the models are quite big): product.rb class Product < ActiveRecord::Base attr_accessible…
Morred
  • 51
  • 1
  • 3
5
votes
1 answer

ruby on rails - how to make relationship works in route, controller, view ? has_many, belongs_to

I am struggling to get my relationship in rails to work. I have a User,Gallery,Comment model class Gallery has_many :comments belongs_to :user end class User has_many :comments has_many :galleries end class Comment belongs_to…
Axil
  • 3,606
  • 10
  • 62
  • 136
5
votes
1 answer

Frustrated with Visual Studio 2012 on Windows Server 2012

Im trying to keep to best practises and not run as the machines administrator, so im running under a user account, marked as an administrator. VS2012 needs to be run as administrator, so ive marked the shortcut as 'run as administrator'. However,…
maxp
  • 24,209
  • 39
  • 123
  • 201
5
votes
5 answers

How can I minimize the 'contain' queries in CakePHP?

I have three models, Users, Comments and Pages. Users has many Comments, and Comments belong to Pages. All models use the containable behavior, and default to recursive -1. If I call a find() query on Comments, with the contain request including…
Rhys
  • 1,581
  • 2
  • 14
  • 22
5
votes
3 answers

Product orders between 2 users

I have three models: User, Product, Offer and a problem with the relationship between these models. Scenario: User 1 posts a product User 2 can send User 1 an offer with an price e.g $ 10 User 1 can accept or reject the offer My questions are…
5
votes
1 answer

Grails (GORM) Many-To-One Cascade Delete Behaviour

I've been struggling to produce the right configurations to produce cascade-delete behaviour in a relatively simple Grails project. Say I have the following simple domain classes: class Author { String name static constraints = { …
Glennn
  • 467
  • 1
  • 7
  • 18
4
votes
2 answers

ror - include foreign key at both ends of has_many and belongs_to?

I'm inheriting code which has: class Graphic < ActiveRecord::Base has_many :comments, :foreign_key => 'asset_id', :conditions => 'asset_type_id = 5', :order => 'created_at', :dependent => :destroy class Comment < ActiveRecord::Base …
Michael Durrant
  • 93,410
  • 97
  • 333
  • 497
4
votes
1 answer

CakePHP hasOne/belongsTo model relationship

I have a few models I'm trying to relate. One model is Item, one is Slide, and another is Asset. Items have multiple slides beneath them. Assets are basically files that have been uploaded (images, mp3s, etc) and slides are where assets are…
theraccoonbear
  • 4,283
  • 3
  • 33
  • 41
4
votes
1 answer

activescaffold belongs_to relationship giving routing error

I am using the following: Rails 3.0.3 Vhochstein's Fork for Activescaffold rake 0.9.0 ruby 1.9.2 I have a model called component which has a belongs_to relationship with category. This was modelled using activescaffold and was working quite well. I…
4
votes
1 answer

Golang Gorm not retrieving data from associated table

I'm working on a Gin app using Gorm with MySQL. In order to define a belongs to relationship in a Gorm Model, you have to do the following (example taken from Gorm docs): // `User` belongs to `Company`, `CompanyID` is the foreign key type User…
MrCujo
  • 1,218
  • 3
  • 31
  • 56
4
votes
2 answers

passing object for polymorphic lookup parameter in Rails find/where

Let's say I have: class Comment < ActiveRecord::Base belongs_to :commentable, :polymorphic => true end class Article < ActiveRecord::Base has_many :comments, :as => :commentable end class Photo < ActiveRecord::Base has_many :comments, :as…
Bashir E
  • 131
  • 2
  • 6