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

Why is my has_one database relationship not working in rails?

I have a relationship model in which students and employers can enter into a relationship when applying to a project. Here are the models: class Student < User has_many :relationships, dependent: :destroy has_many :employers, through:…
0
votes
1 answer

CakePHP 2.4: Setting up models with both a HABTM assoc. and a belongsTo assoc. via a model alias and subsequently have saveAssociated() work

I'm stuck on what I've overlooked (or fundamentally misunderstood!) when trying to build this: Accounts HABTM Users and Administrators belongsTo Accounts, where Administrator is an alias for User. I am pretty awful at RDBMS so to further…
Jonline
  • 1,677
  • 2
  • 22
  • 53
0
votes
1 answer

ActiveRecord::Associations::CollectionProxy HABTM create/build not working

I have three models. Two are related through a has_and_belongs_to_many association with the appropriate join table and one with an has_many association. class Item < ActiveRecord::Base has_and_belongs_to_many :users has_many :colors end class…
0
votes
1 answer

Mass assignment exploit for HABTM checkboxes in Ruby On Rails

I am using HABTM checkboxes as described here: http://railscasts.com/episodes/17-habtm-checkboxes Problem is not all categories are available for a project, but using this method a user can inspect the code in their browser, and change the category…
0
votes
1 answer

CakePHP: created and modified fields not updating in join table

We have two models which are related by a has and belongs to many (HABTM) relationship: Jobs, Tests. We are able to add/edit the relationships successfully (we know because they show up in the join table), but their created and modified fields are…
emersonthis
  • 32,822
  • 59
  • 210
  • 375
0
votes
1 answer

Rails - controller query with includes, where and AND

I have posts habtm users and habtm tags. I need to find posts, that have both tags "cats" and "cute", so I'm trying to use: current_user.posts.includes(:tags).where("tags.title = ? AND tags.title = ?", "cats", "cute") and it returns an empty array,…
0
votes
1 answer

Creata a HABTM Object in Console

I cant figure out how to create a relationship between a player and roster through a HABTM relationship called players_rosters, but in my console how do i create a relation between the two with attributes. Players_rosters consists of roster_id and…
Pierre
  • 1,114
  • 2
  • 10
  • 24
0
votes
1 answer

rails habtm single category model but populate and filter by two select box in view

okay, if you have a habtm relationship between category and articles, now through the category index page is it possible to perform a filter, using two select box populated with the categories, which shows articles belonging to either or both…
curiousCoder
  • 192
  • 1
  • 17
0
votes
1 answer

rails show category select box and redirect based on the selection

i am trying to find a way in which a select box is populated with categories and when the user selects a category and submits, they are redirected to the selected categories page which show all listings tagged with that category. basically its an…
0
votes
1 answer

HABTM now with additional data in join table?

I'm new to CakePHP and I want to save additional data to my HABTM join table. I've read, that one should switch to hasMany through for this, but there is also the following note in the cake cookbook: Changed in version 2.1. You can set unique…
Nareille
  • 811
  • 2
  • 11
  • 30
0
votes
1 answer

RAILS HABTM checkboxes don't update

I am trying to realize HABTM checkboxes following this tutorial: http://www.justinball.com/2008/07/03/checkbox-list-in-ruby-on-rails-using-habtm/ While everything seems to work nicely the updates are not saved to my database. My controller looks…
0
votes
0 answers

Cakephp multiple checkbox HABTM relationship in one line (horizontal)

I'm developing an e-commerce website. I have 3 tables Products (id, name, subcategory_id) colors_products(id, color_id, product_id) Colors (id, name) in my add product form (add.ctp): Form->input('Color',…
batrisya
  • 7
  • 1
  • 7
0
votes
3 answers

rails habtm relation

I have 2 models which have a has_and_belongs_to_many relation: class Category < ActiveRecord::Base has_and_belongs_to_many :templates end class Template < ActiveRecord::Base has_and_belongs_to_many :categories end I want to know how can I get…
datisdesign
  • 3,165
  • 8
  • 30
  • 29
0
votes
2 answers

MySQL - counting with HABTM relationships

I have 3 tables: 'cards', 'tags' and 'cardstags', where cards HABTM tags Question: What query do I execute on the 'tags' table to count the number of associated 'cards' rows? I'm looking for something like this: tags.name |…
cardflopper
  • 976
  • 2
  • 12
  • 19
0
votes
1 answer

ASP.Net MVC HABTM Association

I must be missing something, when trying to define a HABTM association with 2 of my models. I have a "Product" table, and a "Category" table, and a "ProductsCategories" join table. In SQL Server, I am defining the Relationship between the tables…
user35288