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

rails has_and_belongs_to_many update create

I have two models: class Topic < ActiveRecord::Base has_and_belongs_to_many :tags end class Tag < ActiveRecord::Base has_and_belongs_to_many :topics end I make a model for create them both. class Forms::NewTopic include ActiveModel::Model …
0
votes
1 answer

CakePHP virtual HABTM table/relation, something like fixture

first of all I'd like to tell you that you're terrific audience. I'm making an application where I have model Foo with table Foos. And I'd like to give Foo another parameter, HABTM parameter, lets say Bar. But I'd rather don't create table for Bar.…
Sebastian Piskorski
  • 4,026
  • 3
  • 23
  • 29
0
votes
0 answers

Model HABTM Model Referencing Original Model

I have a Team model that is HABTM Match and when I pull the data for a specific Team, I ask for the related Teams for those Matches, but Cake only returns the data for the original Team. How can I get all the Teams (the opponent) for that Match…
Benjam
  • 5,285
  • 3
  • 26
  • 36
0
votes
1 answer

Laravel Database\Eloquent\Collection requires parenthesis on get()

I am using Laravel 4 and returning a belongsToMany relationship MY MODEL class User extends SentryUserModel implements PresentableInterface { public function getPresenter() { return new UserPresenter($this); } public function tweak() { …
user391986
  • 29,536
  • 39
  • 126
  • 205
0
votes
1 answer

Creating assoc record in HABTM through

Models: cities.rb: has_many :cities_users has_many :users, :through => :cities_users I have a HABTM (through) between cities and users. I want to view all cities associated with a user. Here's what I have and what the error is: users.rb has_many…
ale
  • 11,636
  • 27
  • 92
  • 149
0
votes
1 answer

Rails - Only pull in some HABTM associations on a case-by-case basis to avoid unnecessary joins

In Rails 4, I have a project in which I've set up three models with the following many-to-many relationships: An Item has_and_belongs_to_many categories has_and_belongs_to_many tags A Category has_and_belongs_to_many items A…
0
votes
1 answer

How to save data if you have two HABTM relations in a single model?

I have altogether three models: User, Album and Genre. User refers to artist(singer, compose, musician etc). An album can have many artists(users) and many genres. An artist can compose/sing for many albums.In short I have following models and…
0
votes
1 answer

How can I setup a HABTM relationship between a model and several other models using a single table?

I'm having difficulty wrapping my mind around the necessary associations for the following situation: I have an "upload" which can be attached to either a "person", a "group", a "project", or an "office". An upload needs to belong to multiple…
0
votes
1 answer

how to override HABTM table in rails

I am having using rails 3 and have an HABTM(has_and_belongs_to_many) between users <-> emails Also user have roles defined for users as subs, pubs which have a Model that references to itself for mapping. I want to send some emails to…
0
votes
1 answer

HABTM with extra attributes on nested form

I have a products that have and belong to many product_links class Product < ActiveRecord::Base has_many :map_product_prices has_many :product_links, :through => :map_product_prices accepts_nested_attributes_for :product_links and... class…
Sean
  • 1,078
  • 14
  • 25
0
votes
3 answers

Rails ActiveRecord id = xyz works but id != xyz does not work

This works: @user = User.find(current_user.id) @users_cities = @user.cities This does NOT work: @other_users = User.where("id != ?", 1) @users_cities = @other_users.cities However, I can run the second example from the console and it works…
ale
  • 11,636
  • 27
  • 92
  • 149
0
votes
1 answer

I want a core data entity which could relationally point to itself with many-to-many relationships. Category entity

I want to build a category entity in core data. entityName=Category. It has 2 relationships called categoryParents and categoryChildren Those relationships are both configured as "to-many" and they are reverse of each other. They both destined to…
Add080bbA
  • 1,818
  • 1
  • 18
  • 25
0
votes
4 answers

Doing something before saving a form used with HABTM

I currently have 3 tables. snippet tags snippet_tags I'm using HABTM. So I did a form to save a snippet with tags. Keywords are in a text field, separated by commas. What I need to do is to take the string from this text field, loop on the keywords,…
Tommy B.
  • 3,591
  • 14
  • 61
  • 105
0
votes
1 answer

CakePHP HABTM relationship saving new associations

Im having some problems with saving my HABTM relationship for some code I'm working with. I have Podcasts and Categories in a HABTM relationship. Code and problem explanation below Model/Podcast.php class Podcast extends AppModel{ public…
Bizarro181
  • 142
  • 1
  • 12
0
votes
0 answers

CakePHP find() on HABTM association table with missing model

I'm building an e-commerce site using CakePHP 2.3.x that has a Product model associated with an Order model in a HABTM relationship and I'm using orders_products as my join table. I have all of the associations working correctly and doing a find()…