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
6
votes
4 answers

Rails: Find rows without connection in HABTM relation

I have two models, Users and Leads connected with HABTM relation: class Lead < ActiveRecord::Base has_and_belongs_to_many :users end class User < ActiveRecord::Base has_and_belongs_to_many :leads end How can I now get only those Leads which…
Kerozu
  • 673
  • 5
  • 15
6
votes
2 answers

How can I delete an entry from a HABTM join table in rails?

Through many iterations of testing, I just noticed that my join table that represents a HABTM relationship between two models isn't removing entries when instances of these models get deleted. Do I need to do something special when removing an…
6
votes
2 answers

Rails - how to order a model by its has many association?

I have two models: #Product class Product < ActiveRecord::Base has_and_belongs_to_many :categories attr_accessible :name ... end #Category class Category < ActiveRecord::Base has_and_belongs_to_many :products, :order => "name ASC" …
Kulgar
  • 1,855
  • 19
  • 26
5
votes
1 answer

HABTM mongoid following/follower

Mongoid ships with .push on a habtm, which sets a habtm relationship in both directions. Although delete will #delete an associated record, there's no documented way to delete only a relationship that I have seen. Is there a better way of doing…
Peter Ehrlich
  • 6,969
  • 4
  • 49
  • 65
5
votes
6 answers

ActiveAdmin - implement HABTM / multidimensional array in DSL

In my application everything works fine, but in my Active Admin backend I don't get my user roles displayed on screen. I have two models "User" and "Roles": class Role < ActiveRecord::Base has_and_belongs_to_many :users, :join_table =>…
5
votes
1 answer

Implementing tags in Rails: How to reference multiple items with one tag?

I'm writing a blog engine in rails, and have set up a tag model and a post model which have a have_and_belongs_to_many relationship. Tag addition works fine, the problem comes when looking for all posts with a specific tag: If I add tag "test" to…
Chazu
  • 1,010
  • 1
  • 9
  • 20
5
votes
0 answers

Laravel Jensseger Mongodb belongsToMany additional fields

the documentation says "The belongsToMany relation will not use a pivot "table", but will push id's to a related_ids attribute instead. This makes the second parameter for the belongsToMany method useless. If you want to define custom keys for your…
5
votes
2 answers

MySQL INTERSECT via joins table?

So essentially I have two tables, containing URLS and TAGS, with a has-and-belongs-to-many relationship between the two via a joins tables TAGS_URLS. A simple query to find URL's by tags would be: SELECT urls.id FROM urls INNER JOIN tags_urls ON…
Steve C.
  • 73
  • 6
5
votes
5 answers

CakePHP - problem with HABTM paginate query

Tables restaurants cuisines cuisines_restaurants Both restaurant and cuisine model are set up to HABTM each other. I'm trying to get a paginated list of restaurants where Cuisine.name = 'italian' (example), but keep getting this error: 1054:…
Dave
  • 28,833
  • 23
  • 113
  • 183
5
votes
1 answer

Cake HABTM Query, Order By Rand()

I'm aware that Cake HABTM associations are tricky at the best of times, but I seem to be making life even harder for myself... If I want to return a random Item from the db, I can do it as follows in the Item model: $random = $this->find('first',…
thesunneversets
  • 2,560
  • 3
  • 28
  • 46
5
votes
1 answer

Rails: How to observe join records that don't actually have a Model?

Is it possible, using an Observer, to observe the creation of JOIN records? For example, you have a User Model that has_and_belongs_to_many Book Models. Is it possible to monitor books_users records as they are created or deleted or must I have a…
5
votes
4 answers

Rails has_many through cannot save in nested form due to id = null

Using Rails 4.1.13 and Ruby 2.0.0 (although I had the same problem with Ralis 4.0 and Ruby 1.9.3. I have read numerous articles about this particular issue and cannot understand why my solution (which seems exactly like this) does not work, so…
Christoffer
  • 2,271
  • 3
  • 26
  • 57
5
votes
1 answer

Rails 4 scope on HABTM association

I'm a rails newbie and I created a scope on a HABTM association, but I still think it looks unnatural, not elegant, so I think there must be a better way of doing it. Could anyone advise me if there is such better way? I've seen other posts where…
Donato Azevedo
  • 1,378
  • 1
  • 13
  • 22
5
votes
1 answer

Rails 4.2 saving has_and_belongs_to_many association Ids

I have the following bit of code that is working fine with Rails 4.1 with protected_attributes gem (I didn't have my code moved to strong_parameters yet) models/employee.rb class Employee has_and_belongs_to_many :skills attr_accessible…
5
votes
1 answer

Rails Associations, habtm? Polymorphic? Both?

In my Rails app I have three models, Projects, BlogPosts and Images. Projects and BlogPosts can have many linked images and an image can be linked to a Project, a BlogPost or both. What is the best way of setting up the associations to make this…
philnash
  • 70,667
  • 10
  • 60
  • 88