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
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 …
61
votes
4 answers

Does accepts_nested_attributes_for work with belongs_to?

I have been getting all kinds of conflicting information regarding this basic question, and the answer is pretty crucial to my current problems. So, very simply, in Rails 3, is it allowed or not allowed to use accepts_nested_attributes_for with a…
Nick M
  • 939
  • 1
  • 8
  • 9
54
votes
2 answers

Rails belongs_to many models

I did find some questions on SO about Rails associations that are somewhat like my question, but for the life of me I can't seem to understand how to use belongs_to multiple models. Here's the table structure I intend to have: User id Post id …
Zabba
  • 64,285
  • 47
  • 179
  • 207
38
votes
2 answers

Eloquent where condition based on a "belongs to" relationship

Let's say I have the following model: class Movie extends Eloquent { public function director() { return $this->belongsTo('Director'); } } Now I'd like fetch movies using a where condition that's based on a column from the…
Lior
  • 5,454
  • 8
  • 30
  • 38
35
votes
4 answers

Rails: belongs_to vs has_one

A bit of a newbie question on rails associations. I have a Bug model, and a Status model. Status is basically just a key/value pair table. Out of the choices available, I would say Bug has_one Status makes the most sense. However, according to…
Matt Briggs
  • 41,224
  • 16
  • 95
  • 126
27
votes
3 answers

Rails: Non id foreign key lookup ActiveRecord

I want ActiveRecord to lookup by a non-id column from a table. Hope this is clear when I give you my code sample. class CoachClass < ActiveRecord::Base belongs_to :coach end class Coach < ActiveRecord::Base has_many :coach_classes,…
19
votes
4 answers

Nested form in active_admin with select or create option

We are using active_admin for our administration backend. We have a model "App" that :belongs_to model "Publisher": class App < ActiveRecord::Base belongs_to :publisher end class Publisher < ActiveRecord::Base has_many :apps end When creating…
Carsten
  • 593
  • 5
  • 12
19
votes
3 answers

How it works - `belongs_to :user, dependent: :destroy`

I know how to work has_many :posts, dependent: :destroy. If User or something that has_many posts is destroyed, all belonging posts are also destroyed. But what happens when Post model belongs_to :user, dependent: :destroy? I found the option in…
ironsand
  • 14,329
  • 17
  • 83
  • 176
16
votes
4 answers

Undefined property: Illuminate\Database\Eloquent\Relations\BelongsTo::$name laravel 5.4

Hi following are my relations User Model public function loginlogout() { $this->HasMany("App\Models\LoginLogoutLogs"); } and this is my LoginLogoutLogs Model public function users() { return…
noobie-php
  • 6,817
  • 15
  • 54
  • 101
14
votes
2 answers

Laravel Relationships

I've been looking over relationships in Laravel 4 in the documentation and I'm trying to work out the following. I have a table in my database called 'events'. This table has various fields that mainly contain ID's that relate to other tables. For…
Gareth Daine
  • 4,016
  • 5
  • 40
  • 67
14
votes
1 answer

Nested attributes for belongs_to association rails

I have two models, Complaint and Company. Complaint belongs_to and accepts_nested_attributes for Company, and Company has_many Complaints. # Models class Complaint < ActiveRecord::Base attr_accessible :complaint, :date, :resolved belongs_to…
pjmil
  • 2,087
  • 8
  • 25
  • 41
13
votes
3 answers

What are the default values for Rails 3 for :dependent on has_many and belongs_to

In rails 3, i know that i can force deletion of dependent objects on belongs_to and has_many relations using the :dependent => :delete option. However i was wondering, what is the default behavior if i do not specify :dependent => ... Cheers, Hajo
12
votes
2 answers

Rails has many and belongs to one

I have a User model which has many projects and a Project model which can have many users, but also belongs to a single user (ie the user who created this project). It must belong to a User. It also allows a list of users to be associated with it,…
Lee Jarvis
  • 16,031
  • 4
  • 38
  • 40
10
votes
1 answer

Grails many-to-many belongsTo

I want a many-to-many relationship. Then i have to specify a belongsTo at one side like: static belongsTo = Answer But i already have specified a belongsTo as a Map: here the Code class Answer { String text static hasMany = [users:User,…
user1200271
  • 142
  • 2
  • 8
10
votes
2 answers

Setting a :has_many :through association on a belongs_to association Ruby on Rails

I have three models, each having the following associations: class Model1 < ActiveRecord::Base has_many :model2s has_many :model3s end class Model2 < ActiveRecord::Base belongs_to :model1 has_many :model3s, :through => :model1 # will this…
Rohit
  • 5,631
  • 4
  • 31
  • 59
1
2 3
50 51