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

Ruby HABTM association loop

i want to achieve a nested loop without duplicates in a have and belongs to many relationship i have a model 'campaign' and for each campaign i also have campaign data. i want to display each campaign with its campaign data in a table.…
0
votes
2 answers

Rails: What is the correct Association for these models?

What is the correct association for this problem? There are three models: Residents Parties Addresses Each Resident and Party has an Address Each Address can belong to a Resident or Party or to both. There can be multiple Parties at the same…
0
votes
0 answers

Sequelize: Filter ManyToMany Association Through Pivot Table Field

I've 3 models: A, B, and its pivot, AsBs. I'm trying to get all A/B entities (querying its respective repositories) where pivot's boolToCheck is true. I've been able to filter through each other model's fields (ex: A where B.bField is whatever), but…
0
votes
2 answers

Impossible to save product_id key in has_and_belongs_to_many table ? Key is auto-incremeting

I'm getting confused with the creation of a new link in a has_and_belongs_to_many table. I think my use of the .build(...) is not correct but I can't find a way to fix it. I wrote : @user = User.find(1) if (params[:product_id]) …
Stéphane V
  • 1,094
  • 2
  • 11
  • 25
0
votes
1 answer

Has_many through has_many relation issue in rails

In my gaming project I have defined couple of models: class Ticket belongs_to :user belongs_to :game has_and_belongs_to_many :payments, join_table: 'tickets_payments', association_foreign_key:…
0
votes
1 answer

Save has_and_belongs_to_many link in basic RoR app

I try to learn the has_and_belongs_to_many relationship between my 2 fresh new and simple models Product and Author, where a Product can have many authors and where author can have a lots of products. I wrote this : class Author <…
0
votes
2 answers

CakePHP: How to setup BelongsToMany with the same table?

I have a table called processo_administrativo that has and belongs to many processo_administrativo through processo_administrativo_processo_administrativo Both tables have id "identity(1,1)" The join table,…
celsowm
  • 846
  • 9
  • 34
  • 59
0
votes
2 answers

Laravel 7 Eloquent: Comment BelongsTo User belongsToMany Groups

I am trying to setup the following relationsship: Comment (user_id) ->belongTo User ->belongsToMany ->Groups (Pivot: group_user) I want to be able to fetch all groups associated with the comment with: Comment::find(i)->groups. Also…
Line in Linus
  • 390
  • 8
  • 25
0
votes
2 answers

CakePHP transparent saving of unique data

I have models Person and Phone/Email with HABTM relationship. After some pain I found out, that my life is easier, when I break HABTM into: Person hasMany PeoplePhone, Phone hasMany PeoplePhone, PeoplePhone belongsTo (Person,Phone). Well, I don't…
Petr Cezar
  • 159
  • 2
  • 8
0
votes
2 answers

Laravel 7 Single row echo belongsToMany relationship

I'm trying to display the value of brand_id column from brands table. Here's so far what I've done: Car model use App\Brand; class Car extends Model { public function brands(){ return $this->belongsToMany(Brand::class); } } Brand…
Kevin
  • 315
  • 2
  • 5
  • 19
0
votes
0 answers

How to add a virtual field on belongsToMany->setForeignKey?

I’ve need to create on-the-fly a association between the same table, processo_judicial, where another table contains reference to “both”. My current code is: $pjTable =…
celsowm
  • 846
  • 9
  • 34
  • 59
0
votes
1 answer

best way to save dependent objects in a has_and_belongs_to_many relation?

Hi I am new to rails and I would like to know what is the best way who save dependent objects in an HBTM relation. Specifically, I have two classes Post and Tag class Post < ActiveRecord::Base has_and_belongs_to_many :tags end class Tag <…
cgiacomi
  • 4,629
  • 6
  • 27
  • 33
0
votes
1 answer

How to setup 2 aliased has_and_belong_to_many relationships with one class?

I am developping an authorisations maganement, based on Attributes and Roles Based Access Control. I need to associate Roles to a Group (of users), and available Features to a Group (of users). Neither the Role class nor the Feature class exist,…
user1185081
  • 1,898
  • 2
  • 21
  • 46
0
votes
1 answer

How to setup has_and_belongs_to_many relationship with a scoped and aliased class?

I try to associate Roles to a Group (of users). The Role class does not exist, I use an extract (a scope) of the Parameter class. I got inspiration from apidock.com, but cannot implement the example using a scope. I setup the relationship as…
user1185081
  • 1,898
  • 2
  • 21
  • 46
0
votes
1 answer

Indices on `has_and_belongs_to_many` join-tables

When using a join-table [:left_id, :right_id] in Rails, what indices should it have? I googled for the answer and have only found that people suggest single index containing both keys. But in most cases there's no need to search by both keys…
RocketR
  • 3,626
  • 2
  • 25
  • 38