Questions tagged [has-and-belongs-to-many]

A has_many :through association set up a many-to-many connection with another model.

In Ruby on Rails, a has_many :through association is often used to set up a many-to-many connection with another model. This association indicates that the declaring model can be matched with zero or more instances of another model by proceeding through a third model.

1372 questions
9
votes
1 answer

Rails: HABTM - find all records with no association

I have 2 models (Workout, Equipment) in a has and belongs to many relationship. If I use Workout.find(:all, :joins => :equipment, :conditions => "equipment.id = 5") it works, but if I use Workout.find(:all, :joins => :equipment, :conditions =>…
9
votes
2 answers

HABTM association associated to single table inheritance

I have a product model that has many sections, and a section can belong to many products. The section model has subclasses of Feature, Standard and Option. My models are: class Product < ActiveRecord::Base has_and_belongs_to_many :categories …
rick
8
votes
4 answers

Best way to specify a default record for a has_many relationship in rails

I have Accounts and AccountAddressess. An account can have many AccountAddressess and I would like to specify one as the "default_account_address", so in the Account table, I have a column named "default_account_address_id". Here is the current…
Shagymoe
  • 1,296
  • 1
  • 15
  • 22
8
votes
2 answers

Rails 5 before_destroy throw(:abort)

I'm working in a legacy Rails app that has been diligently upgraded with each major version of Rails and we're currently on rails 5.1 and I can't get a before_destroy to prevent the deletion if it fails a validation I've been reading that return…
Sparkmasterflex
  • 1,837
  • 1
  • 20
  • 33
8
votes
1 answer

Is there a RESTful way to configure routes for habtm?

In Rails you can use nested routes to create RESTful routes for has_one and has_many relationships. Examples can be found on the Rails Guides I'd like to ask if there is a good way to configure RESTful routes for habtm relationships? For example if…
liangzan
  • 6,804
  • 4
  • 29
  • 28
8
votes
2 answers

Rails 4 has_and_belongs_to_many doesn't work properly with includes statement

Recently I ran into mystical bug with rails 4 and HABTM relation. first of all my Gemfile: source 'https://rubygems.org' gem 'rails', '~> 4.1.6' gem 'pg' Next. my Models: class User < ActiveRecord::Base end class Teacher < User …
8
votes
1 answer

rails prevent deletion of child unless parent is being deleted also

in Ruby on Rails 4, let's say a parent has many children. When the parent is deleted, the children must also be deleted. Other than that, the child shall not be deleted unless it is an orphan. How to do that? I tried with the following class Parent…
8
votes
1 answer

How to cleanly update a list of HABTM associations in Rails 4?

I'm trying to update a User record with the list of associated Addresses, connected through the has and belongs to many association. The request body I'm sending through javascript is: {"id":10,"name":"John Smith", "address_ids":[4,8]} This is…
8
votes
3 answers

ActiveAdmin won't save has many and belongs to many field

I have 2 models. Category and Post. They are connected using a has_many_and_belongs_to_many relationship. I checked in the rails console and the relationship works. I created checkboxes in activeadmin to set the post categories using this form…
Allanon
  • 547
  • 4
  • 24
8
votes
1 answer

How to create has_and_belongs_to_many relationship with Ember.js & ember-data?

Is it possible to create a hasAndBelongsToMany relationship with Ember.js & ember-data? Edit: Added ActiveRecord model examples to clarify. class Project < ActiveRecord::Base has_and_belongs_to_many :tags end class Tag < ActiveRecord::Base …
David
  • 1,330
  • 17
  • 31
7
votes
1 answer

Rails has_and_belongs_to_many is confusing me with fixtures and factories

General Confusion I have bands which can have 3 genres. I read in a previous SO post that the proper way to handle this is a couple steps: 1) In band.rb has_and_belongs_to_many :genres 2) Create a band_genres join table Even after reading the…
Tony
  • 18,776
  • 31
  • 129
  • 193
7
votes
2 answers

Default_scope on a join table

I've got a model setup like the following: class User has_many :items has_many :words, :through => :items end class Item belongs_to :user belongs_to :word default_scope where(:active => true) end class Words has_many :items end The…
7
votes
4 answers

How do I query data in CakePHP using HABTM relationships?

I'm working on a CakePHP 1.2 application. I have a model "User" defined with a few HABTM relationships with other tables through a join table. I'm now tasked with finding User information based on the data stored in one of these HABTM tables.…
Daniel
7
votes
1 answer

Join table comment in rails 4 migration

I'm pretty new on rails 4 and I'm not really sure about how should be my join_table. I've done the migration rails g migration CreateJoinTableQuestionSubTopic question sub_topic And I get this file class CreateJoinTableQuestionSubTopic <…
7
votes
3 answers

CakePHP Fatal Error Call to a member function schema() on a non-object

I have some trouble finding a solution for this.. Error: Call to a member function schema() on a non-object File: /Cake/Model/Model.php Line: 3627 In my Database there are the tables articles,hashtags and the association articles_hashtags with the…
1 2
3
91 92