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

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

4
votes
3answers
66 views

BelongsToMany relation. How to get unique rows

I have next 'member_companies' table in DB: And in model Member has a relation : public function companies() { return $this->belongsToMany(Company::class, 'member_companies'); } And it ...
0
votes
1answer
14 views

Edit relationship of parent record in activeadmin

I have the following model structure: Composition has many Score (Score belongs to Composition) Composition has and belongs to many Countries (and viceversa) score.rb: class Score < ...
0
votes
0answers
16 views

Prevent patchEntity() from removing duplicates

I'm trying to save an entity with a BelongsToMany associated, i.e. Order BelongsToMany Products. My joint table contains important data that I need to save, even if Products ids are duplicated. No ...
0
votes
1answer
46 views

Creating Join Table in Rails with One Model and Two Attributes on Another Model

Attempting to create a join table that takes two attributes on one model, and associates them with the id of a second model. Cannot use the id of the first model due to a constant refresh process, ...
0
votes
0answers
26 views

Sequelize: belongsToMany association through a join table

This is the error I am getting when I call product.getAttributeValues(): Unhandled rejection SequelizeDatabaseError: Unknown column 'ProductAttribute.attribute_id' in 'field list' I don't know ...
2
votes
1answer
49 views

Rails HABTM ActiveRecord::RecordNotFound in update action

I have set an association using HABTM with Rails, with two models: Site and User. Site.rb has_and_belongs_to_many :fae_users, class_name: "Site", foreign_key: "site_id", join_table: "...
0
votes
1answer
33 views

ActiveRecord::StatementInvalid on two namespaced models

I have to two models in the same namespace which have a habtm relation. class Resource::Item < ApplicationRecord has_and_belongs_to_many :resource_sets, foreign_key: 'resource_item_id', ...
0
votes
0answers
44 views

Sequelize belongsToMany ignores additional attributes in join table

I have two models: Article and DescriptionFragment in a BelongsToMany association through a join table Descriptions, which in turn BelongsTo another model Category and also has an attribute "sequence"...
0
votes
0answers
25 views

saving values of hasMany relationship

i´m looking for a way to store entered values ​from a belongsToMany relationship. What I intend to do. The main entry takes place under "assessments". Mapped into socialskills in this example. So ...
0
votes
2answers
38 views

How should I access a virtual attribute from an associated model?

I have two models which are associated through has_and_belongs_to_many relationship. I have created a virtual attribute :full_name from the first model's(User) :first_name and :last_name attributes. ...
0
votes
2answers
54 views

How to assign one model's record to another associated record in active admin?

I have an app which registers applicants as Participants and then assigns them to Groups. The two models are associated via has_and_belongs_to_many relationship. There are other hanging models ...
0
votes
1answer
15 views

How do I resolve an invalid statement when deleting a record that is associated using has_and_belongs_to association?

I have a join table, which I didn't name using traditional Rails conventions. It joins Participant and Group models (associated using has_and_belongs_to) and would normally be named ...
0
votes
1answer
44 views

How do I get associated records to show in active admin?

I have a registration app which records a Participant model, which has two hanging models- Student_Detail and Volunteer_Detail. Participant has_one of Student_Detail and Volunteer_Detail. There is a ...
0
votes
1answer
27 views

How can I remove duplicates results when using belongsToMany

Having the following tables: ----------- ----------------- --------------- | PROJECT | | ACCESSES | | ENVIRONMENT | ----------- ----------------- --------------- | id | ...
0
votes
1answer
27 views

Can't save data from another table. Ruby on Rails 5

I have a flights table and passengers table. When creating a new flight I need to be able to add multiple passengers to that flight. I have managed to join my flights and passengers tables and the ...
0
votes
0answers
29 views

Laravel relational queries - ManytoManytoMany

In my query I need to couple my Workflow model to the currently logged in Client via two linked tables: ClientCompany and CompanyWorkflow. Getting the base-query written down wasn't a problem: DB::...
1
vote
3answers
47 views

Factory Bot HABTM nil value in join table

I am getting a (frustrating!) error when running rspec (with Factory Bot) tests for my Rails app. I'm seeing the following error: ActiveRecord::NotNullViolation: Mysql2::Error: Field '...
0
votes
0answers
19 views

sequelize create + belongsToMany association + through

Trying to create an instance and associating the required instances and the additional row data in one command A.belongsToMany(B, through: models.AB) B.belongsToMany(A, through: models.AB) define('AB'...
0
votes
2answers
24 views

Order HABTM associations by created_at on the association instead of the object

I have a Match and User model with a has_and_belongs_to_many between them. How do I retrieve match.users.first and match.users.second based on when the MatchUser association was created, rather than ...
1
vote
0answers
57 views

Laravel Pivot Table insert data

I have three tables. I want to add a oyuncu (player) to a grup (group), so I created a method (public function oyuncustore(Request $request, $grup_id)). But I get an error and I can't add rows to ...
0
votes
0answers
23 views

Mongoid override has_and_belongs_to_many getter, not setter

I want to override my mongoid HABTM getter, but keep default << setter. Here's what i have for the getter: has_and_belongs_to_many :users alias original_users users def users ...
0
votes
2answers
191 views

Laravel Eloquent load relationship of the pivot

In my website I have User, Roles and Permissions. Each user belongs to many Roles (eg. User.php) and pivot table called 'role_user' public function roles() { return $this->belongsToMany(Role:...
0
votes
0answers
42 views

CakePHP 3: Saving and retrieving a nested BelongsToMany association

I have a complex association in my db. I'm trying to create a new record in targets. Every target has many products and every product has many options. Join tables both have a joinData column (points)...
0
votes
2answers
91 views

Laravel Eloquent exclude specific result from belongsToMany() query

On the User.php model I have the following relationship: public function roles() { return $this->belongsToMany(Role::class); } In the database, I have different roles and basically I want to ...
1
vote
1answer
350 views

Laravel Nova field BelongsToMany drilling down the data

I am developing a Web Application using Laravel. I am using Laravel Nova for building the admin panel. But I am having an issue with BelongsToMany field. I have the database schema as follow Area ...
0
votes
1answer
35 views

ActiveRecord no longer finding duplicate IDs in array and saving for has-and-belongs-to-many relationship, Rails 4.2.8

I upgraded from rails 4.2.0 to rails 4.2.8 this evening. The only major pain has been figuring out why my array of item_ids can no longer have a duplicate when saving in my items_payments HABTM ...
1
vote
2answers
97 views

Rails 5 has_and_belongs_to_many form

I have a little app that has a clients and a sites model. I want to create a new site in a modal from the show page but i get and error. Please find below schema, controller, models and error. ...
0
votes
1answer
13 views

Perform initial loading in node in hasAndBelongsToMany relation

How I can make a initial load data in a hasAndBelongsToMany relation ? Example: Model Product { idProduct, Description (..) } Model Order { idOrder, Date (...) } In the swagger have: PUT: /order/{...
1
vote
1answer
467 views

A class was passed to `:class_name` but we are expecting a string

I'm trying to create a join table called :books_users where a column in books, :claim, is a boolean, where if a person clicks the link to "review this book", the claim action in the books controller ...
2
votes
0answers
156 views

Sequelize BelongsToMany set foreignKey as primaryKey

I have relation N:M between User and Role, also I have association named UserRole, where I am storing few attributes and two foreignKeys: user_id and role_id. My goal is to set user_id in UserRole as ...
1
vote
1answer
67 views

How do I dynamically chain where clauses in rails?

I have a Videos model and a Tags model. Tags HABTM Videos and Videos HABTM Tags. class Tag < ApplicationRecord has_and_belongs_to_many :videos scope :with_tag, -> (id) { where(id: id) } end ...
1
vote
1answer
56 views

Rails error: `block in assert_valid_keys': Unknown key: class

I have a working Rails project. I'm adding a feature that adds a secondary relationship between two tables. They have a primary join table, and I'm adding a second one for a different relationship: ...
6
votes
2answers
219 views

Complete course and modules using Rails 5 assign to user

Edit #2 Here is the courses controller class CoursesController < ApplicationController layout proc { user_signed_in? ? "dashboard" : "application" } before_action :set_course, only: [:show, :...
2
votes
1answer
148 views

Laravel: How to use where clause in many to many relation

How i can use where clause in many to many relation ship Table: Users id - name - email Table: Shiftings id - name Table: shifting_user id - user_id - shifting_id User Model: public ...
1
vote
1answer
122 views

Rails 5 HABTM Unpermitted parameters

I'm throwing in the towel after a day of getting nowhere (and the answer will likely be annoyingly simple, but that's code). Ok, I have 2 models, User (with Devise) and Chapter. Models: class User &...
0
votes
2answers
99 views

Cakephp 3 - save data to belongstomany table

In Cakephp3, I have the following tables: CREATE TABLE `users` ( `id` int(11) NOT NULL, `name` varchar(45) DEFAULT NULL, `created` datetime NOT NULL, `modified` datetime DEFAULT NULL ) ENGINE=...
0
votes
1answer
71 views

RoR lookup HABTM join by multiple ids

I've got 3 tables -- a model QuizResult, a model QuizAnswers and then a jointable for them (quiz_answer_quiz_results): class QuizResult < ActiveRecord::Base has_and_belongs_to_many :...
1
vote
0answers
18 views

Sequelize belongsToMany with second belongsToMany table - set at once

I have the following tables const Employee = db.define('employee', { ... }) const CapabilityType = db.define('capabilityType', { ... }) const Capability = db.define('capability', { id: {type: ...
0
votes
1answer
89 views

Polymorphic has_and_belongs_to_many

How define a has_and_belongs_to_many polymorphic association? Situation: Figure that we have users, tracks, lists, etc... and all these models can be tagged and use this tag for filter. What i'm ...
0
votes
1answer
41 views

In Jbuilder, How Can you Generate an Array of IDs Without the 'ID' Key?

I have the following Jbuilder setup but I'd like to output the 'uw_question_ids' as values only. No 'id:' key. Is this possible? json.menu do json.uw_question_ids menu.uw_questions do |uw_question| ...
0
votes
1answer
38 views

rails 5 belongs_to or has_and_belongs_to_many?

I've been out of the loop for a while and humbly request a refresher. I would like to list all Partners under their respective PartnerCategories in an accordion style toggle <div class="...
0
votes
1answer
34 views

Grouping two records and associating the group through a HABTM in ActiveRecord | Rails

I'm working on a Rails 5.2 project that stores and manages guest lists. I have Event and Guest models, and they are associated with a HABTM relationship. This works well, but there is a requirement to ...
0
votes
2answers
35 views

Laravel BelongtoMany relation save extra column with the two model in relation

i try to build form to create invoice and then generate it pdf My Db structure tables: -- invoices contain date | invoice_num | client_id -- articles contain name | description | price -- ...
0
votes
1answer
160 views

HABTM and has_many through association issue with multiple database

I am trying to implement the HATBM association between the two models of which one model points to my local database table and other model is pointing to the table of an external database. local ...
0
votes
1answer
156 views

Sequelize: Create join table attribute with set<Association>

I am working with Sequelize 4.38.0 and I have a problem with the belongsToMany association. I defined my tables like this: const Role = db.define('role', { name: {type: Sequelize.STRING, allowNull:...
0
votes
1answer
149 views

ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: relation with HABTM association with rails

I have two models enterprise and deal_event with habtm association. This relation works with create and when I do Enterprise.last.deal_events but throws error ActiveRecord::StatementInvalid: PG::...
1
vote
1answer
431 views

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 ...
1
vote
3answers
273 views

filter Sequelize belongsToMany get<Association> association table

I am working with Sequelize 4.37.10 and it works great. Unfortunately the documentation is not perfect in my opinion. So it lacks a bit of describing the belongsToMany possibilities. I have the ...
0
votes
1answer
23 views

Save data in association table with same entities (belonsToMany) Cakephp 3

I have Campaigns and Actions. Each Campaign can has unlimited actions (even can be repeated). Cakephp documentation say you can save the asociation with actions._ids but I have a problem with this. ...
-1
votes
1answer
44 views

ruby on rails - has and belongs to many relation and routes

I'm creating project with projects and users in many to many relation. I would like to have view where I can add projects to user by id or name. I know this is linked with routes, I tried out a lot ...