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
0
votes
1 answer

Has many through or Has and belongs to many missing ID

Just trying to create a simple workout log here I'm getting this error when I try and create weights "Workout ID must exist" Models class Workout < ApplicationRecord belongs_to :user has_many :exercises has_many :weights, through: :exercises …
0
votes
2 answers

Bi-Directional has_and_belongs_to_many on a Single Model in Rails

I have User model and I want to create friendship between users. I've achieved this using has_and_belongs_to_many has_and_belongs_to_many :friends, class_name: 'User', foreign_key: :friend_id And I have table for this association: create_table…
0
votes
1 answer

Add records with HABTM

I am trying to allocate multiple categories to products using the below method class Category < ActiveRecord::Base has_many :categories_products has_many :products, :through => :categories_products validates :name, presence: true,…
Boss Nass
  • 3,384
  • 9
  • 48
  • 90
0
votes
1 answer

What's Wrong With My Sequelize belongsToMany Search?

I'm trying to use Sequelize joins for the first time. I seem to have done everything exactly as per the docs, but for some reason my related records aren't being returned by the search. SEQUELIZE MODELS const apptsModel = db.define('Appts', { …
VikR
  • 4,818
  • 8
  • 51
  • 96
0
votes
1 answer

The association `has_and_belongs_to_many` has an option 'uniq'?

I am learning rails with the book 'Rails 4 in Action', and it says the association has_and_belongs_to_many has an option uniq. But it seems it doesn't work as it says. The class below means only unique tags should be retrieved for each ticket, but…
0
votes
1 answer

Rails, can't load data from fixtures that have HABTM relationshionship

I'm new to rails and testing and have a problem with loading my fixtures. payment.yml payment:   name: something   amount: 1.5   event: some_event   user: some_user   description: long text   users: some_user,…
0
votes
0 answers

Defining setter in model in context of has_many-belongs_to assoication in RoR

Now I am teaching myself with the book "Rails 4 in action", and got stuck at some point. Let me describe my problem. I have 3 models that are a state, a ticket and a comment. Both the ticket and the comment belong to the state. The comment belongs…
Sookie J
  • 823
  • 2
  • 8
  • 16
0
votes
1 answer

In Rails, how can I setup my HABTM when I have two models on different databases?

I have two models as outlined below. I have a joins table I want to specify, but it errors out since they're not in the same database. If I specify my development database in my "join_table:" options hash then it works. But of course that would…
daveomcd
  • 6,367
  • 14
  • 83
  • 137
0
votes
1 answer

Rails - newbie association question with both has_many and HABTM models

I have three models: class Tenant < ActiveRecord::Base has_many :sites end class Site < ActiveRecord::Base belongs_to :tenant has_and_belongs_to_many :users end class User < ActiveRecord::Base has_and_belongs_to_many :sites end For Site…
0
votes
1 answer

Ruby on Rails HABTM Multiple Dropdowns with AJAX

I have a typical HABTM relationship between members (actual members of our organization, not all have online accounts) and users (online accounts). I have an edit users page for site administrators where multiple members can be assigned to…
please delete me
0
votes
2 answers

Why am I getting a “Not Associated” error even though the HABTM association is in place?

I'm making some tournament scoring software for my bike polo league. The goal is to match up teams in such a way that everyone gets a chance to play every other team before repeat matches start. In my Match Entity class, I have a function called…
Kluny
  • 189
  • 3
  • 17
0
votes
0 answers

Creating a Parent object whose HABTM accepts_nested_attributes_for children is ignoring the association(children) attributes update

My ActiveRecord Models: class Parent < AR has_and_belongs_to_many :children accepts_nested_attributes_for :children, reject_if: :all_blank, allow_destroy: true end class Child < AR has_and_belongs_to_many :parents end The controller for…
0
votes
1 answer

Ember Model with Nested hasMany or belongsTo relationship

If you want to setup a model with a belongsTo or hasMany relationship, you can do the following: export default Model.extend({ "client": DS.belongsTo('client',{ async: true }), "days": attr(''), "cars": DS.hasMany('car',{ //…
Matt
  • 1,811
  • 1
  • 19
  • 30
0
votes
1 answer

paginating habtm relations

got this situation. Reports habtm users. So Im trying to paginate just the Reports that are linked to the Auth user... I read when you have a habtm relationship you have to bind the model temporarily using 'hasOne' like this: function…
juan
  • 21
  • 4
0
votes
1 answer

fail to create propper habtm scope

I stuck (.I have two models with habtm relationship. create_table "daily_menus", force: :cascade do |t| t.string "day" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.date "date" end …
anti-k
  • 317
  • 2
  • 13