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

How to save duplicate records use belongsToMany?

I've 2 models: Templates and Modules. Both models have association belongsToMany to the other. Now I want to save few modules associated with Template (modules can be use multiple times for one template). I alsa have additional field (position) in…
0
votes
1 answer

Containable unexpected behavior in related models

We have the Ebooks HABTM Tags. And we try to select the Ebooks that have belong to the tag with id=160. Both use the containable behavior, so in the Ebooks Controller I have written the following: $this->Ebook->contain('Tag.id = "160"'); $ebooks =…
Dimitris S.
  • 115
  • 7
0
votes
1 answer

Unpermitted parameter in HABTM association

I'm trying to define a has_and_belongs_to_many association between two models in my Rails 5 app (api-only in case that somehow matters). The models are bags and products, and the association works as expected from the console - I can type…
0
votes
1 answer

Rails Roles / Permissions in a HABTM Relationship -- managing the relationship

I have created the following tables in my application - roles, permissions and permissions_roles. Roles HABTM permissions, and permissions HABTM roles. I have seen many tutorials on setting up all of the tables/models etc for the 3 tables and…
0
votes
2 answers

belongsToMany with nullable foreign - Laravel 5.8

in my Laravel project i get this database structure: Products Id Name Orders Id Total Order_Product Product_id (nullable) Order_Id Details In my Order model I make belongsToMany retaltionship with Product model: public function products() { …
0
votes
1 answer

Rails has_and_belongs_to_many association

so I have two models: class User < ActiveRecord::Base has_and_belongs_to_many :followed_courses, :class_name => "Course" end class Course < ActiveRecord::Base has_and_belongs_to_many :followers, :class_name => "User" end in User.rb, I also…
randomor
  • 5,329
  • 4
  • 46
  • 68
0
votes
2 answers

Rails habtm grief am I dumb or rails bug? (desperate ; )

Greetings all, I'm having a weird problem with a habtm relationship and honestly I'm beginning to think I may have stumbled upon some weird bug in rails 3. Surely I'm crazy though. I've been beating my head against the wall on this for 3 days,…
Gerald
  • 93
  • 1
  • 6
0
votes
3 answers

How to fix error with has_and_belongs_to_many creation in rails

I want to create a record in join table but rails shows me two errors in two situation, and I don't want to generate a third model. @channel = Channel.find(params[:channel_id]) if @channel.users.create!(channel_id: params[:channel_id], user_id:…
0
votes
1 answer

cakephp habtm join issue

I have table structure like below: events (boxing, sparring etc) competitors (users who are participating into diff events) events_competitors (every competitor's selected events will go here) Now what i want is, as per my match schedules i want to…
Aditya P Bhatt
  • 21,431
  • 18
  • 85
  • 104
0
votes
1 answer

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, that reorders the rows on that model's table…
0
votes
1 answer

How can I save additional information to a CakePHP HABTM Join Table?

Currently, I'm unable to save a record to a HABTM join table manually. I have a table, users_products and would like to save all additional information about the order (size, quantity, color, etc...) to this join table, instead of creating a…
Nick
  • 1,311
  • 2
  • 10
  • 27
0
votes
1 answer

CakePHP: HABTM + 1

I have a site that scrapes all the episodes from tv.com from certain series. So my database has a User table, a Show table, an Episode table, a Show_User table (to associate shows with users, HABTM), an Episode_Show table (to associate episodes with…
theweirdone
  • 21
  • 1
  • 2
0
votes
0 answers

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 there is also a corresponding table and also the…
0
votes
2 answers

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. I am using ActiveAdmin, and trying to show the…
0
votes
2 answers

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 related to Participant, but they are not connected to…
dbate
  • 127
  • 13