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

Refactoring view loops to use fewer queries via includes with a HABTM relationship

I have been trying to refactor this code to reduce the db calls by possibly using "includes". I would like to replace the three nested loops in the view. Tried various options but got stuck... I'm still getting familiar with active record querying. …
0
votes
1 answer

It is possible to replace an inner join query by a containable in a habtm relationship?

I read the section Containable and I didn't find a clear example to replace an inner join query on a habtm relationship by a containable query. Example : Model Student hasAndBelongsToMany Teacher Teacher hasAndBelongsToMany Student Query $joins =…
Eric Lavoie
  • 5,121
  • 3
  • 32
  • 49
0
votes
1 answer

Rails 4: what's wrong with this scope?

In Rails 3.2 this worked fine: class Component < ActiveRecord::Base has_and_belongs_to_many :brands ... scope :branded, ->(b) { includes(:brands).where('brands.id in (?)', b.id) } Other end of the relationship: class Brand <…
t56k
  • 6,769
  • 9
  • 52
  • 115
0
votes
0 answers

CakePHP hasAndBelongsToMany errors when writing to JoinTable

I have Products mapping to Images via the join table Products_Images Product Model: class Product extends AppModel { public $hasAndBelongsToMany = array( 'Image' => array( 'className' => 'Image', 'joinTable' =>…
Chronix3
  • 601
  • 2
  • 9
  • 21
0
votes
1 answer

Rails: HABTM records not being deleted appropriately

I'm doing maintenance work on an existing rails site and have run into a strange bug. There are a few different models involved here: Registrations, Business Categories, and Services. Registrations HABTM Business Categories and Services, each of…
justinbach
  • 1,945
  • 26
  • 44
0
votes
4 answers

Cakephp, i18n, Retrieve translation records for associated models

Quoting from the cakephp Book (ver 1.3): Note that only fields of the model you are directly doing find on will be translated. Models attached via associations won't be translated because triggering callbacks on associated models is currently not…
ion
  • 1,033
  • 2
  • 16
  • 46
0
votes
1 answer

accepts_nested_attributes with HABTM relationship

class OptionValue has_and_belongs_to_many :listings, join_table: 'listing_options' belongs_to :type class Listing has_and_belongs_to_many :option_values, join_table: 'listing_options' accepts_nested_attributes_for…
0
votes
2 answers

What query do I perform to access the last of a collection of models before they have been saved?

I have a Workflow model, an Action model, and a Role model. Actions are nested attributes of a workflow, and an action has and belongs to many roles. The associations work fine. However, in my form view, I need to add a role to the last action that…
0
votes
1 answer

Cakephp, Retrieve Data for HABTM Models using conditional find

There are 2 Models: Project & Category that are bind with HABTM relationship. I would like to perform a search from projects controller that can do the following: FIND all DISTINCT Project.scedule WHERE Category.slug != 'uncategorised' Apologies…
ion
  • 1,033
  • 2
  • 16
  • 46
0
votes
1 answer

How to show only rows which have no HABTM association in table

I have a news feed app I'm writing and I would like to figure out a way to be able to mark each news article as having been "viewed" by each user individually. My current method I am trying to implement is to consider any rows in the articles_users…
Ezra Free
  • 808
  • 11
  • 21
0
votes
1 answer

cakephp security component HABTM (multiselect)

I'm using Cakephp v 2.5.1 with the Security component. I have a profile form where I have a HABTM relationship between a user and projects where the user can select 0-N projects (basically a multiselect). When posting, the post gets…
0
votes
2 answers

Rails 4 HABTM Select2

Having a small problem saving client_ids. I don't understand where i'm going wrong. Basically i have 3 models Client, Message and Delegateship. I would like to send a message to multiple clients at the same time. I am trying to use select2. class…
Benjamin
  • 2,108
  • 2
  • 26
  • 46
0
votes
1 answer

UI: What are the different ways to display a Has and Belongs to Many relationship?

This is not a question about a specific framework. I am using plain php with jquery. I am trying relate multiple products to multiple sets of options. That is each product can have multiple non-exclusive sets of options related to them. What would…
Parris
  • 17,833
  • 17
  • 90
  • 133
0
votes
1 answer

has_one based on attributes from a has_and_belongs_to_many relationship on the same model - ruby on rails

I have the following models: class Audit < ActiveRecord::Base has_one :country, :through => :another_model has_and_belongs_to_many :questions end class Question < ActiveRecord::Base has_and_belongs_to_many :audits has_many :details # one…
bensmithbwd
  • 343
  • 3
  • 12
0
votes
1 answer

Refactor HABTM with checkbox

i wanna refactor this code: <%= hidden_field_tag "contact[group_ids][]", nil %> <% Group.all.each do |g| %> <%= check_box_tag "contact[group_ids][]", g.id, @contact.group_ids.include?(g.id)%> <%= label_tag g.name %>
<% end…