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
-1
votes
1 answer

Laravel - Add additional where constraints from Eloquent Model belongsToMany relationship

I am new to Laravel (using v7) so I apologise if I am doing this incorrectly. I have a custom subscription setup for my users. I have my users table id first_name last_name 1 John Doe 2 Jane Doe I have my subscriptions…
odd_duck
  • 3,941
  • 7
  • 43
  • 85
-1
votes
1 answer

How to get posts by current tag and category simultaneously in Laravel?

I have Post, Tag and Category models. I know how to get posts by tag or posts by category. But I need to get posts by chosen tag and category simultaneously (at one time). Can somebody help? I use Laravel 8. Post model class Post extends Model { …
SlimBoy Fat
  • 71
  • 1
  • 3
-1
votes
1 answer

hasManyThrough relations ships with join?

I have 4 tables (Cars, Vignettes, Inspections and Insurances). Vignettes, Inspections and Insurances are "belongsTo" to Cars. I need to combine results of these three tables into one query and sorting by 'created_at' and take first 3-4 of them. How…
-1
votes
1 answer

Laravel belongsToMany pivot with belongsTo

How can get relation Beetwen 3 tables enfants [id,nom,prenom] responsables[id,nom,prenom] relations[id,libelle] // père, mère, frère ... responsable_enfants[id,enfant_id,responsable_id,relation_id] Model Enfant.php public function…
-1
votes
1 answer

Can't display categories under category type

I have two different models category and category type i just want to display categories under category type but i don't know how. I’m using has_many and belongs_to association where the category has a foreign key 'categorytype_id' I've tried but…
-1
votes
2 answers

Saving HABTM records when not all join table columns are foreign keys

I am trying to update tables with a has and belongs to many (HABTM) relationship. When my join table looked like this: CREATE TABLE IF NOT EXISTS `items_labels` ( `item_id` int(11) NOT NULL, `label_id` int(11) NOT NULL ) ENGINE=MyISAM DEFAULT…
ryonlife
  • 6,563
  • 14
  • 51
  • 64
-1
votes
1 answer

Rails HABTM validation

Validation for HABTM is not working, I used the following code: Class GroupsUsers < ActiveRecord::Base belongs_to :user belongs_to :group validates_uniqueness_of :user_id, :scope => :group_id end Does anyone have a solution as to why this…
lamrin
  • 1,431
  • 4
  • 22
  • 34
-1
votes
1 answer

BelongsToMany with BelongsToMany relation

I need to take data from a relation belongsToMany with belongsToMany, it would come to be so: A->B->C in my model would be providers->caption->eventType, so y need to take all providers from an event type. model looks like: Providers Model Class…
Alan Flores
  • 35
  • 1
  • 8
-1
votes
1 answer

how to save has many through association in controller in rails?

i have a Projects model and User model. i want to establish has many through association between these models. now my requirement is i want to list all the users in the multiselect drop down, in projects/new form, by this i want to assign all users…
-1
votes
1 answer

HABTM form validation with CakePHP 2.x

I have a HABTM relation like : Post <-> Tag (a Post can have multiple Tag, and same the other way). This work with the multiple checkbox selection generated by Cakephp. But I want to have at least one Tag for every Post and throw an error if…
Blag
  • 5,818
  • 2
  • 22
  • 45
-1
votes
1 answer

Adding records to has_and_belongs_to_many relation

So I want to implement many to many relation between these two models: class Bet < ActiveRecord::Base belongs_to :betting_event has_and_belongs_to_many :gambler_bets validates_presence_of :odd, :outcome, :description, :betting_event_id…
-1
votes
1 answer

nested resources rails has and belongs to many

Sorry for the generic title, but I'm not sure how to phrase it better at the moment. I finally had some time to start picking up rails again and came across this issue as I was building my models: Essentially I have a parent resource that has two…
-1
votes
1 answer

How to insert data to tables which is in "has and belongs to many" relationship. Controller and Model generated by scaffold in rails

I am new to Ruby on Rails. I use scaffold to generate first controller ( model and view), Projects in this case. Scaffold automatically create a form that can use to insert data to project table. I want to add skills to this form, so users can add…
-1
votes
3 answers

CakePHP select many to many (HABTM) records

after create a many to many (HABTM) relationship between 2 classes (as code bellow) I was expecting to see results including the related records but for some reason this is not happening. Models App::uses('AppModel', 'Model'); class NewsCategory…
PH.
  • 377
  • 2
  • 9
-1
votes
1 answer

rails HABTM versus view (formtastic)

I have two models: The model NetworkObject try to describe "hosts". I want to have a rule with source and destination, so i'm trying to use both objects from the same class since it dont makes sense to create two different classes. class…
VP.
  • 5,122
  • 6
  • 46
  • 71
1 2 3
91
92