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 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 groups_participants. I have tried to destroy a record in the Group…
dbate
  • 127
  • 13
0
votes
1 answer

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 passengers are now showing up in the new flight…
0
votes
0 answers

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::table('workflows AS w') …
BRO_THOM
  • 824
  • 9
  • 24
0
votes
1 answer

Alternative to mass assignment for HABTM

I have a similar situation to this question, which was already posted. Koraktor has asked I'm using a simple model for user authorisation with two ActiveRecords User and Role User and Role have a HABTM relation to each other. . . Manually…
0
votes
2 answers

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 by when the User itself was created?
0
votes
1 answer

CakePHP HABTM relationship, setting related records always adds new entry in jointable even if already exists

This is my code: $charities = explode(',',$this->data['Charity']['charities']); foreach ($charities as $key=>$charity){ $data['Charity'][$key] = $charity; } $this->Grouping->id =…
Will
  • 1,893
  • 4
  • 29
  • 42
0
votes
2 answers

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::class); } and each Role belongs to many Permissions…
Daniel R.
  • 629
  • 1
  • 10
  • 28
0
votes
0 answers

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). I have no problem populating products_targets…
0
votes
2 answers

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 return all the roles, except the "superadmin" role so…
Daniel R.
  • 629
  • 1
  • 10
  • 28
0
votes
1 answer

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 relationship. This works just fine in 4.2.0... but now,…
0
votes
1 answer

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/{id}/product/rel/{fk} { "id": "string", …
0
votes
2 answers

CakePHP multiple HABTM relationships

I have an assets table which contains all the fields shared by all asset types, e.g. name, path, filetype, size, etc. Now, I have 2 asset types: assets belonging to architects and assets belonging to construtors. So I set up HABTM relationships on…
dianovich
  • 2,288
  • 21
  • 34
0
votes
1 answer

How to access information using Rails in Join Model

I have two models: USERS has_many :celebrations has_many :boards, :through => :celebrations BOARDS has_many :celebrations has_many :users, :through => :celebrations CELEBRATIONS :belongs_to :user :belongs_to :board I understand that I create a…
chell
  • 7,646
  • 16
  • 74
  • 140
0
votes
1 answer

Ruby on Rails , habtm collect method not collecting?

I am trying to backup data to a yaml store on deletion like so: DeleteProject.create!(:data => { :project => project.attributes, :domains => project.domains.collect(&:attributes), :databases =>…
Hard-Boiled Wonderland
  • 1,359
  • 3
  • 17
  • 32
0
votes
2 answers

Incorrect object returned from a has_and_belongs_to_many relationship

I have two models, User and Discussion User model has_and_belongs_to_many :subscribed_discussions, :class_name => 'Discussion', :join_table => 'discussions_subscriptions' has_many :discussions Discussion model has_and_belongs_to_many…
Dogbert
  • 212,659
  • 41
  • 396
  • 397