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
2 answers

HABTM Association Build

I need propagate this values in :departaments_products table: , but I received the error: I'm using Rails 4 NoMethodError in Products#new undefined method `departament_id' for # view.html.erb: <%= form_for(@product) do |f|…
0
votes
1 answer

How do I write a has_many :through a scope by a particular ID?

My model reads as so : class Registration < ActiveRecord::Base has_many :registered_courses has_many :courses, through: :registered_courses has_many :sections, through: :courses The problem is that if many different Registration's have the…
Trip
  • 26,756
  • 46
  • 158
  • 277
0
votes
1 answer

How can I query my db for params by "and" instead of "or"

I have habtm relation between products and colors. When I preform a query on products that are "red" and "black" I want it to return product that have "red" AND "black" associations not "red" OR "black" This is my scope for this query: scope…
bonum_cete
  • 4,730
  • 6
  • 32
  • 56
0
votes
2 answers

Correct methods in Ruby on Rails model

My Rails model has some bad lines. In general, first error is when accessing roles variable from has_access def. And then, second is when making where action. class Organization < ActiveRecord::Base has_and_belongs_to_many :organizations_users …
0
votes
1 answer

Rails has_one and has_many association on the same model

I have a users model and a groups model. A user has_and_belongs_to_many :groups and a group has_and_belongs_to_many :users. The associations and join tables work correctly. A group can only have one user who owns the group, I want to name be able…
raphael_turtle
  • 7,154
  • 10
  • 55
  • 89
0
votes
1 answer

Find query not showing results from referenced table in CakePHP using HABTM

I have a HABTM relationship between two models, Agents and Categories. I've followed the instructions here: http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html I've setup the tables, created the FKs, etc. In order to test…
tokidote
  • 13
  • 3
0
votes
0 answers

How do I query a table with multiple HABTM associations

I have an ItemsDesign model with a HABTM with ItemsCategory and Color, belongs to ItemsStyle and Store The Page is set up where you would select a category or subcategory. then you would have the ability to filter by color, store, style and price…
bonum_cete
  • 4,730
  • 6
  • 32
  • 56
0
votes
1 answer

How to perform COUNT with HABTM and left join?

Considering the following associations: class Pool < ActiveRecord::Base has_and_belongs_to_many :participations end class Participation < ActiveRecord::Base has_and_belongs_to_many :pools end I want to get the number of participations in each…
0
votes
2 answers

Deleting from join table in CakePHP

I am having a problem where I have tables users and teams, my relation is defined as below: public $hasAndBelongsToMany = array( 'Teams' => array( 'className' => 'Team', 'joinTable' => 'teams_users', 'foreignKey' =>…
Ondrej Rafaj
  • 4,342
  • 8
  • 42
  • 65
0
votes
1 answer

rails modelA has_many modelB, modelB habtm modelC, how to display the modelAs associated with a single modelC?

It's not necessarily a has_many through but sorta maybe? let's say modelA = Car Make let's say modelB = Models let's say modelC = Features so I started with something like: class CarMake < ActiveRecord::Base has_many :models end class Model <…
0
votes
2 answers

Rails join and HABTM relationship

In my application I have model Car which: has_and_belongs_to_many :locations Now I'm buidling searching and I want search Car which has given locations. In my view I have: .row = horizontal_simple_form_for :cars, {url: cars_path, method: :get} do…
0
votes
1 answer

Permissions wont assign more then one?

I have a really basic model relationship of has_and_belongs_to_many, through: 'table_name' Which we can see here: Roles module Xaaron class Role < ActiveRecord::Base extend FriendlyId friendly_id :role, use: [:slugged, :history] …
user3379926
  • 3,855
  • 6
  • 24
  • 42
0
votes
1 answer

CakePHP HABTM find first record which has certain related records

I have a HABTM relation between Games and Participants. I need to find a Game which has Participants with IDs 1 and 2 "bound" to it. How to achieve this? I tried with $options['conditions']['participant_id'] = array('1', '2'); $game =…
Oliver
  • 3,981
  • 2
  • 21
  • 35
0
votes
1 answer

Has_many through form

I am hoping for some help with constructing a form a has_many :through relationship. I have three classes: order.rb has_many :selections has_many :items, :through =>:selections; item.rb has_many :selections has_many :orders, :through =>…
0
votes
1 answer

Cakephp saveAssociated and SaveAll Nothing Working Properlly

I am developing my application with cakephp 2.4.6. i know there are lots of posts in the stackoverflow related to saveAssociated issue.I have implemented several HABTM relationship in my database and now i am struggling a lot because of its…
SibinF
  • 385
  • 1
  • 7
  • 25