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

Multiple Associations to the Same Model in CakePHP 3

I am using cakePHP version 3.x. When i query the MessagesTable i want to get the Users data for the the sender and the receiver of the message. I have done this many times in cakePHP 2 but i cant figure out why it isn't working in version 3.x. i…
Wisd0m
  • 521
  • 1
  • 5
  • 14
8
votes
6 answers

Explain belongsTo in Grails

From the Grails belongsTo documentation, what is the use of class Book { static belongsTo = Author } What is the impact of cascading operations on Book, when CRUD operations performed on Author? EDIT: Thanks for your responses, may be i…
CSR
  • 770
  • 1
  • 14
  • 30
8
votes
1 answer

can Belongs_to work without has_many or has_one

I am studying Belongs_to association, I have used following models, in that every order belongs to the customer, so I have used belongs_to in order model it giving error while creating order undefined method `orders' for # when I use has_many…
Sampat Badhe
  • 8,710
  • 7
  • 33
  • 49
6
votes
1 answer

Why is my user_id nil?

def destroy @dignity.destroy end Sorry, that's not code, that's just how I feel right now. I know there are a ton of beginner questions on Devise, I think I looked at almost every single one. I have a very simple Devise setup in Rails 3. I…
Kevin
  • 1,489
  • 2
  • 20
  • 30
6
votes
1 answer

Can "touch" be used on a belongs_to polymorphic relationship in Rails?

I expected the following to work: class Attachment < ActiveRecord::Base belongs_to :attachable, :polymorphic => true, :touch => true end which I expect the associated objects to be "touched" when the Attachment record is saved or destroyed. It…
Dia Kharrat
  • 5,948
  • 3
  • 32
  • 43
6
votes
2 answers

Deeply nested Rails forms using belong_to not working?

I'm working on a messy form which among other things has to manage a section with two-level nesting. It's almost working, but there's a snag and the only thing I can see that's different from other deeply-nested forms that work is that there's a…
Masonoise
  • 1,573
  • 3
  • 14
  • 28
6
votes
1 answer

How to filter by foreign id and local attribute via belongs_to?

The following models are linked via belongs_to: require 'mongoid' class Sensor include Mongoid::Document field :sensor_id, type: String validates_uniqueness_of :sensor_id end ... require 'mongoid' require_relative 'sensor.rb' class…
JJD
  • 50,076
  • 60
  • 203
  • 339
6
votes
1 answer

ExtJS 4: Understanding hasMany and belongsTo

I've been struggling to understand how to use hasMany and belongsTo for quite sometime. My understanding is hasMany is a 1:many relationship and belongsTo is a many:1 relationship--aside: so does that imply that if you have a hasMany relationship, a…
incutonez
  • 3,241
  • 9
  • 43
  • 92
6
votes
1 answer

Can't get accepts_nested_attributes_for to work for two levels deep?

I have three models Game > Team > Players and I want to be able to submit the following to add a game along with multiple teams and players on those teams. {"game"=>{"name"=>"championship", "teams_attributes"=>[ {"result"=>"won",…
avian
  • 1,693
  • 3
  • 20
  • 30
6
votes
2 answers

Undefined method for a belongs_to association

My code: class User < ActiveRecord::Base belongs_to :university end class University < ActiveRecord::Base has_many :users, dependent: :destroy end and my model User has a university_id attribute. If I do University.find(1).users I get the…
marimaf
  • 5,382
  • 3
  • 50
  • 68
5
votes
2 answers

Optional or Conditional model associations in Rails

I have a user model. Users can have 1 of 3 roles: role1, role2, role3. This is represented by a 'role' column in the user model. Each role has a unique profile. role1_profile, role2_profile, role3_profile. Each *_profile is a model. How do I…
hrdwdmrbl
  • 4,814
  • 2
  • 32
  • 41
5
votes
2 answers

ActiveRecord Associations: Any gotchas if has_many WITHOUT corresponding belongs_to?

A phone has many messages. An email address has many messages. A message either belongs to a phone, email, or neither. The belongs_to association is optional. The following associations seem to work fine for these relationships: Phone model…
jpw
  • 18,697
  • 25
  • 111
  • 187
5
votes
1 answer

Does Grails cascade deletes when not using domain.delete()?

From the Grails site: http://www.grails.org/doc/1.0.x/guide/5.%20Object%20Relational%20Mapping%20(GORM).html class Airport { String name static hasMany = [flights:Flight] } class Flight { String number static belongsTo =…
Igor
  • 33,276
  • 14
  • 79
  • 112
5
votes
2 answers

Laravel belongsTo with condition and eager load

I have a Post model associated to a Section model, which depend on an extra condition to work: belongsTo('App\Models\Section', 'id_cat')->where('website',…
lkartono
  • 2,323
  • 4
  • 29
  • 47
5
votes
2 answers

Rails Form with belongs_to Association

I'm very new to Rails so this may be an obvious problem, and if so I apologize. I am trying to create a form for creating a User record, which has a belongs_to association with a Team model. What I've done so far is the following... <% form_for…
Alex P
  • 530
  • 1
  • 6
  • 13
1 2
3
50 51