Questions tagged [has-many]

has_many indicates a master-detail 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 (the relationship will be belongs_to)

1446 questions
29
votes
4 answers

Rails - check if record exists in has_many association

I'm not sure if my question is worded correctly. I have three models: User, Item, and UserItem. user has_many :user_items user has_many :items, through :user_items item has_many :user_items item has_many :users -> {uniq}, through :user_items item…
user4584963
  • 2,403
  • 7
  • 30
  • 62
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,…
25
votes
3 answers

RAILS: How to get has_many associations of a model

how I can get the has_many associations of a model? For example if I have this class: class A < ActiveRecord::Base has_many B has_many C end I would a method like this: A.get_has_many that return [B,C] Is it possible? Thanks!
Pioz
  • 6,051
  • 4
  • 48
  • 67
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
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
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
18
votes
4 answers

Is it possible to create a conditional association in model?

I have setup a role based access controll system with the following models: Role (as STI), UserRole (global roles) ProjectRole (project specific roles) Assignment (Polymorphic with different resources) User Project (as one resource type for…
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
3 answers

Rails Question: belongs_to with STI -- how do i do this correctly?

I've been playing around with STI and belongs_to / has_many relationships and I'm a bit confused. I have a few questions based on a model configuration similar to: class Parental < ActiveRecord::Base end class Mother < Parental has_many…
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

Need data from rails join table, has_many :through

I have 3 tables - users, things, and follows. Users can follow things through the follows table, associating a user_id with a things_id. This would mean: class User has_many :things, :through => :follows end class Thing has_many :users,…
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
13
votes
1 answer

Delete a Has-Many Relationship ONLY

I have a: has_and_belongs_to_many :friends, :join_table => "friends_peoples". To add a friend I do: @people.followers << @friend which create the relationship and a new person profile. Now I'd like to delete the relationship ONLY and not the person…
Alextoul
  • 839
  • 3
  • 9
  • 19
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
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
1
2
3
96 97