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

Linking HABTM Attribute

I'm currently trying to create a link to a new page which displays all the posts that belong to a certain category. They're both connected via a HABTM relationship as you can see below. Category.rb class Category < ActiveRecord::Base …
Jonathan
  • 673
  • 1
  • 10
  • 32
0
votes
2 answers

Displaying and selecting from a HABTM

Currently I have a Category and Post model, joined by a HABTM relationship. Posts belong to multiple categories and have many attributes. Categories just have a Name Property. How do I create a multi-select form in my Posts _form.html.erb so I can…
Jonathan
  • 673
  • 1
  • 10
  • 32
0
votes
1 answer

CakePHP HABTM won't save relationships

I've been digging through similar questions but couldn't get my app to save HABMT relationsships. I want to have "Test" HABMT "Browser". The Tables: cake_browsers (id, name, ...) cake_tests (id, name, ...) cake_browsers_tests(browser_id,…
Doena
  • 315
  • 4
  • 20
0
votes
1 answer

Rails Habtm with a field

Is that possible to have a field in a has and belongs to many table? Just like favorite: create_table :messages_users, :id => false, :force => true do |t| t.integer :message_id, :null => false t.integer :user_id, :null => false t.boolean…
moshimoshi
  • 251
  • 1
  • 6
  • 15
0
votes
1 answer

Laravel single table inheritance and sync

I'm using simple table inheritance with Laravel (using a plugin like Nanigans/single-table-inheritance or intrip/laravel-single-table-inheritance but I can also implement my own one, that isn't the problem). I'm also using belongsToMany relations: A…
0
votes
2 answers

data is not saved in joins table

I am working on a website, where a user can have multiple projects and multpile users can contribute to a single project. I have a project model class Project < ActiveRecord::Base #associations has_and_belongs_to_many :users end and a users…
0
votes
1 answer

CakePHP HABTM: how to find all Photos HABTM multiple Tags?

I have two tables, photos and tags, with a standard photos_tags link table. My models show Photos HABTM Tags, and I can update both fine with the links updating too. My question is: say I now want to find all Photos tagged with both "Sunset" and…
user204529
  • 149
  • 2
  • 4
0
votes
1 answer

MySQL HABTM - Find most consecutive widgets per user

I currently have the following SQL database: CREATE TABLE `users` ( `id` INT(10) NOT NULL AUTO_INCREMENT, `name` VARCHAR(40), `email` VARCHAR(40), PRIMARY KEY (`id`) ); CREATE TABLE `widgets` ( `id` INT(10) NOT NULL AUTO_INCREMENT, …
QuotidianVoid
  • 609
  • 5
  • 12
0
votes
1 answer

how insert data directly and only into HABTM table ,

I have 1 Category table and second Features table and have association among them is HABTM in cakephp Now i don't know exactly how to insert values in categories_features table. I used HABTM before but at that time i insert values in this table…
coder
  • 156
  • 3
  • 23
0
votes
1 answer

Associating Models with Polymorphic

I am trying to associate Contacts with Classes but as two different types. Current_classes and Interested_classes. I know I need to enable polymorphic but I am not sure as to where it needs to be enabled. This is what I have at the moment class…
0
votes
0 answers

Rails HABTM adding an object

Models: class Booking < ActiveRecord::Base has_and_belongs_to_many :spaces end class Space < ActiveRecord::Base has_and_belongs_to_many :bookings end I've used habtm many as I don't need any other information, just a relationship. The idea…
Ralph King
  • 994
  • 1
  • 9
  • 19
0
votes
1 answer

filter elements from habtm relation

i have the following models: class HealthCareInsurance < ActiveRecord::Base has_and_belongs_to_many :companies end class Company < ActiveRecord::Base has_and_belongs_to_many :health_care_insurances end Now, inside my controller, i need to get…
0
votes
1 answer

Problems with HABTM and ActiveAdmin

I've looked for this exhaustively on StackOverflow and Google but found no answer to help me out, so I'm asking this though I know the subject has been hardly discussed here. I have a HABTM relation between Contests and Subjects, my log shows de…
Rob
  • 2,243
  • 4
  • 29
  • 40
0
votes
2 answers

Active Record query for has_many/belongs_to associations

I have built a Rails app that has data about the US Congress. I have a Legislator model and a Bill model: class Legislator < ActiveRecord::Base has_many :bills end class Bill < ActiveRecord::Base belongs_to :legislator end So bills that a…
user3809888
  • 377
  • 1
  • 6
  • 23
0
votes
0 answers

SQLException in rails 4 has_and_belongs_to_many association

I have a strange SQLException with my association. I`ve created a join table using migration: create_join_table :reviews, :posts So there is a table in db posts_reviews In both models I`ve configured relations: class Post < ActiveRecord::Base …