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

Rails - how would you set up this HABTM relationship

I have a situation where Project can have many Tags (and vice versa), so I have set up a has_and_belongs_to_many relationship between the two. My question is this: I need a project to have the ability to carry a single current tag. Option 1 Can I…
0
votes
1 answer

Eloquent belongsToMany based on type

I faced some problem which I cannot resolve using Laravel's Eloquent. I have two or more (6 - exactly) objects: - ObjectA - ObjectB - ObjectC Each of these objects has a one own Variant, but a variant structure is the same for each Object, so can…
bmbbambus
  • 51
  • 1
  • 6
0
votes
2 answers

Rails: Where none of the associations have a certain value

I have books and tags. def Book < ApplicationRecord has_and_belongs_to_many :tags end def Tag < ApplicationRecord has_and_belongs_to_many :books end I want to find all books that do not have the tag with the id 1 associated. (They may have no…
Railsana
  • 1,813
  • 2
  • 21
  • 30
0
votes
1 answer

Joins Across Several Tables (HABTM, Rails, Ruby)

This is a conceptual question, not a gimme the code question. I'm working with 4 models: Doctor, Specialization, School, Service Doctor has mutual HABTM associations with the other 3 models, and 3 joins tables have been created to hold these…
Ken Corley
  • 13
  • 5
0
votes
1 answer

Ruby On Rails data migration from sqlite3 to mysql regarding to many to many relations

I have a sqlite3 database in my rails app which has a development.sqlite3 file. I want to convert the database to mysql.Here are the steps I take but still I have problems : 1.First I add and install gem 'seed_dump' to add the data into my seed.rb…
0
votes
1 answer

Has and belongs to many Rails API POST request

Currently I have a ReactJS application communicating with a Rails 5 API. As part of this the React application makes a single JSON POST request to api/v1/job to create a new job record. This request includes information for two HABTM relationships…
Tom Pinchen
  • 2,467
  • 7
  • 33
  • 53
0
votes
2 answers

Rails HABTM setup, model object, & join_table insertion controller setup

I have the following setup. 1 product has many product_types. many product_types have 1 type. A HABTM relationship from my understanding of the docs. My models are class Product < ApplicationRecord has_and_belongs_to_many :types end class Type <…
0
votes
2 answers

How to seed database with has_and_belongs_to_many

I have has_and_belongs_to_many relations between two models (Phone and Category) and I seed the phones and categories's tables with something similar to this : Phone.create(name:'Home', phone:'+00 0 00 00 00 00') Phone.create(name:'Work', phone:'+00…
Nino
  • 1,217
  • 2
  • 8
  • 13
0
votes
2 answers

How do I return records associated to a list of children of a nested set?

Rails version 3.0.3, I am new to rails, but been in webdev for a long time. I am using awesome nested set. I have the tables "posts", "labels", and "labels_posts" posts has_and_belongs_to_many labels labels has_and_belongs_to_many posts labels…
Tyler Clendenin
  • 1,459
  • 1
  • 12
  • 25
0
votes
3 answers

Sequelize query belongstomany not including child attributes

I´m new to sequelize and trying to determine how to perform a query on a belongstomany relationship, where I need to check for the existence of the child relationship but I don´t want to bring any of its fields. Project.belongsToMany(models.User,…
0
votes
2 answers

belongsToMany with parent-child relationship in Laravel 5

I have two models that are related to each other with a belongsToMany relationship. The models are Game and Category. Of course, the tables for both are games and categories. The Category model, has its own parent-child relationship. Basically this…
Tales
  • 1,829
  • 3
  • 33
  • 50
0
votes
1 answer

Laravel, How to retrieve parent records with certain Pivot table values belongsToMany

How can I retrieve all records of my model based on certain ID's in my pivot table? I have the following 3 tables users; id, name stats; id, name stats_selected; user_id, stats_id Model User.php public function stats() { return…
Flyhnl
  • 5
  • 1
0
votes
3 answers

CakePHP Multiple Entry Fields w/ saveAll() HABTM multiple records Insert not saving

I've found some very helpful tutorials and posts on StackOverflow about this topic, but I am completely stuck on one point. Everything below IS working with the exception of my HABTM Zip data. Here is my code:
OldWest
  • 2,355
  • 7
  • 41
  • 61
0
votes
1 answer

CakePHP accessing data in distal related tables

I have 2 tables associated as belongsToMany, scouts and merit_badges. I also have data in the join table that I need to access so the relationship options is set as 'through' e.g. in ScoutsTable.php $this->belongsToMany('MeritBadges', [ …
Drew
  • 1
  • 2
0
votes
1 answer

CakePHP paginate results with a condition on another table linked with HABTM

I've done some searching but I can't find anything relevant enough/working for my scenario. I've got: Jobs <--> HABTM (Users_jobs table) <--> Users I would like to do a paginate() from my Job controller with a condition on the User.id, as I do need…