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
223
votes
4 answers

Rails has_many with alias name

In my User model I could have: has_many :tasks and in my Task model: belongs_to :user Then, supposing the foreign key 'user_id' was stored in the tasks table, I could use: @user.tasks My question is, how do I declare the has_many relationship…
doctororange
  • 11,670
  • 12
  • 42
  • 58
106
votes
6 answers

Rails find record with zero has_many records associated

This seems fairly simple but I can't get it to turn up on Google. If I have: class City < ActiveRecord::Base has_many :photos end class Photo < ActiveRecord::Base belongs_to :city end I want to find all cities that have no photos. I'd love to…
Andrew
  • 42,517
  • 51
  • 181
  • 281
93
votes
3 answers

Use a scope by default on a Rails has_many relationship

Let's say I have the following classes class SolarSystem < ActiveRecord::Base has_many :planets end class Planet < ActiveRecord::Base scope :life_supporting, where('distance_from_sun > ?', 5).order('diameter ASC') end Planet has a scope…
Aaron
  • 13,349
  • 11
  • 66
  • 105
76
votes
2 answers

ActiveAdmin with has_many problem; undefined method 'new_record?'

I'm trying to customise a ActiveAdmin form for a Recipe model that has a has_many relationship with Step. class Recipe < ActiveRecord::Base has_many :steps end class Step < ActiveRecord::Base acts_as_list :scope => :recipe belongs_to…
nickpellant
  • 1,046
  • 1
  • 7
  • 9
73
votes
3 answers

Validate the number of has_many items in Ruby on Rails

Users can add tags to a snippet: class Snippet < ActiveRecord::Base # Relationships has_many :taggings has_many :tags, :through => :taggings belongs_to :closing_reason end I want to validate the number of tags: at least 1, at most 6. How…
user142019
72
votes
4 answers

Rails has_many :through Find by Extra Attributes in Join Model

New to both Ruby and Rails but I'm book educated by now (which apparently means nothing, haha). I've got two models, Event and User joined through a table EventUser class User < ActiveRecord::Base has_many :event_users has_many :events, :through…
Stefan Mai
  • 23,367
  • 6
  • 55
  • 61
60
votes
2 answers

Ember js - Hasmany relationships breaks after updating other tables

I am using Ember.js with local-storage-adapter. I have a weird problem while updating records. I have a post and comments model with hasMany relationships: App.Post = DS.Model.extend({ title: DS.attr('string'), comments:…
viji
  • 1,652
  • 2
  • 20
  • 34
51
votes
8 answers

Rails Model has_many with multiple foreign_keys

Relatively new to rails and trying to model a very simple family "tree" with a single Person model that has a name, gender, father_id and mother_id (2 parents). Below is basically what I want to do, but obviously I can't repeat the :children in a…
Kenzie
  • 1,285
  • 2
  • 11
  • 13
50
votes
3 answers

When will ActiveRecord save associations?

I know that it will save associations when autosave: true as per https://api.rubyonrails.org/classes/ActiveRecord/AutosaveAssociation.html I know that it will save associations that are constructed like book = Book.new(name:…
hrdwdmrbl
  • 4,814
  • 2
  • 32
  • 41
40
votes
9 answers

how to avoid duplicates in a has_many :through relationship?

How can I achieve the following? I have two models (blogs and readers) and a JOIN table that will allow me to have an N:M relationship between them: class Blog < ActiveRecord::Base has_many :blogs_readers, :dependent => :destroy has_many…
Sebastian
  • 2,889
  • 4
  • 34
  • 37
40
votes
6 answers

rails model has_many of itself

I have a event model. Events can have parent events, set from a column in the model (parent_event_id). I need to be able to do has_many :event on the model, so I can just do, for example, event.child_event or event.parent_event. But my googling…
Alexander Forbes-Reed
  • 2,823
  • 4
  • 27
  • 44
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
35
votes
4 answers

Rails RSpec Tests for a has_many :through Relationship

I'm new to testing and rails but i'm trying to get my TDD process down properly. I was wondering if you use any sort of paradigm for testing has_many :through relationships? (or just has_many in general i suppose). For example, i find that in my…
Mario Zigliotto
  • 8,315
  • 7
  • 52
  • 71
33
votes
1 answer

How to use constant in the ON condition in Yii2 hasMany relation

I try to create a polymorphic association, what is common in Rails but unfortunately not in Yii2. As part of the implementation I need to define the relation: public function getImages() { return $this->hasMany(RecipeImage::className(), …
Tibor Nagy
  • 1,185
  • 1
  • 15
  • 28
29
votes
5 answers

Rails has_many association count child rows

What is the "rails way" to efficiently grab all rows of a parent table along with a count of the number of children each row has? I don't want to use counter_cache as I want to run these counts based on some time conditions. The cliche blog…
SWR
  • 667
  • 2
  • 6
  • 12
1
2 3
96 97