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

Strongloop - HasAndBelongsToMany always returns 401

I'm trying to execute this request: PUT /api/cars/564d8e792583afef310affe3/categories/rel/suv-idcat This works fine if I'm logged in as administrator, but if I'm logged in as another role, I get a 401 response. My Car model has the following…
danielrvt
  • 10,177
  • 20
  • 80
  • 121
0
votes
2 answers

How to do associations with two user types and one model with has and belongs to many relationships in rails?

I've been trying to solve this problem for a while now. Looked here, but it is not exactly what I need. I have three models: User, Group, GroupMembership. User can be a teacher and a student, so user has different Roles through UserRoles table.…
0
votes
3 answers

How to show articles with special categories?

So I'm using has_and_belong_to_many association for two models (Article and Category). My header have link with dropdown menu where different categories appears. All my articles are on index page and I need to sort it depending what category it has…
SumLare
  • 45
  • 1
  • 5
0
votes
0 answers

How to join one HABTM model by values of another?

I have three models: class Campaign < ActiveRecord::Base has_and_belongs_to_many :models has_and_belongs_to_many :makes class Make < AR::Base has_and_belongs_to_many :campaigns has_many :models class Model has_and_belongs_to_many…
Alex Antonov
  • 14,134
  • 7
  • 65
  • 142
0
votes
1 answer

HABTM through editing via checkboxes

I have a HABTM relationship between Publications and Categories. In the new and edit views, I have this: Categories:
<% @categories.each do |c| %>  <%= check_box_tag :category_ids, c.id, @publication.categories.include?(c), :name =>…
Steve Brewer
  • 2,062
  • 2
  • 14
  • 15
0
votes
1 answer

Rails 4 - Setting up Rolify with devise and pundit - relationships

I am trying to make an app in Rails 4. I use devise for authentication and pundit for authorisations. I want to use Rolify for role permissions. I posted this question and got some really helpful starting out advice.Defining Roles with Rolify I have…
Mel
  • 2,481
  • 26
  • 113
  • 273
0
votes
1 answer

Which data structure (full HABTM ?)

I just want to know which of this two structures is the best for better performance: Full HABTM (many to many), but with a very big join table (so only one association for query) Or, HABTM + 1 hasOne (which reduce significantly the join table rows…
0
votes
1 answer

Rails: Many-to-many relationships with 3 Models

I have a situation where I have Products, Suppliers, ShoppingLists and Valuations. A shopping_list consist of many valuations each with a product, an specific supplier and a price. My models are as follows: class Product < ActiveRecord::Base …
Ben Orozco
  • 4,361
  • 4
  • 33
  • 49
0
votes
1 answer

How to handle Delete link for HABTM association in views?

I've a simple problem but have been struggling with it for many hours. I've these two models in my rails app: class User < ActiveRecord::Base has_and_belongs_to_many :groups end and class Group < ActiveRecord::Base has_and_belongs_to_many…
bosp
  • 107
  • 10
0
votes
2 answers

Laravel 5.1 Eager Loading - belongsToMany with parameter

I have this relationship in my Model: public function modulesData($module) { return $this->belongsToMany($module) ->withTimestamps(); } What I want is to eagerload a dynamic relation of my model. But how can I do this? I use this code…
goldlife
  • 1,949
  • 3
  • 29
  • 48
0
votes
1 answer

CakePHP 3.1, universal BelongsToMany

I made myself a plugin for loading related content with beforeFind(), so you could say that ContentPage/10 is similar to ConentNews/10 and Gallery/5. My table related_contents looks like: id int(11) source_table_name …
Aiphee
  • 8,904
  • 3
  • 18
  • 16
0
votes
1 answer

CakePHP sub query SQL in HABTM relation

I want to suggest related products by tags and sort order by the most matched. the HABTM model association between Product and Tag class Product extends AppModel { //.. var $hasAndBelongsToMany = array("Tag"); //.. } and vice versa in Tag model.…
0
votes
1 answer

Rails checkbox array choose nothing

I m getting the computer_ids of user like that <%= check_box_tag "user[computer_ids][]", computer.id, @user.computers.include?(computer) %> If I choose at least one from checkbox array, that's fine. But if I did not choose any thing it give me an…
Ramazan Zor
  • 209
  • 1
  • 14
0
votes
2 answers

build relationship from list of ids

Hi I have has_and_belongs_to_many relationship between Posts and Comments and in my edit action I have form that returns me list of ids 1,3,5,8 etc. I want build relationship between my current model and all models which ids are in the list so…
Bohdan
  • 8,298
  • 6
  • 41
  • 51
0
votes
1 answer

create action,habtm relation

please i need help this is recipe model: class Recipe < ActiveRecord::Base mount_uploader :cover, AvatarUploader belongs_to :user has_and_belongs_to_many :ingredients,:join_table => "ingredients_recipes" accepts_nested_attributes_for…