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
3
votes
1 answer

Rails 3. HABTM form select drop down menu

I have an invoice form. This is a simplified version: so it has line items where you select a drop down menu of product names. This is working well: So the invoice-line_item relations is this: invoice has_many line_items and line_item belongs to…
leonel
  • 10,106
  • 21
  • 85
  • 129
3
votes
2 answers

How to use HABTM fields in Active Admin filter section?

I'm using Active Admin 0.3.2, and my database schema includes a few has_and_belongs_to_many relationships. I can create and display records just fine, but attempting to include them in the filter section causes things to grind to a…
3
votes
1 answer

Is there a Rails way or a gem to get related entries from HABTM to the same object?

Well, I have a table with images, each image is tagged with a HABTM relation through a join table. What I want to do is show related images in the page the image is being shown, by matching images with the same tags. Can this be done efficiently…
Zequez
  • 3,399
  • 2
  • 31
  • 42
3
votes
2 answers

rails 3 habtm filtering with multiple entries

i have a product model and a product category model. and theres a habtm between those. i have categories like 'men' and 'jeans' and i would like to filter these. to do a filter on men is no problem but i need to filter multiple parameters (men and…
3
votes
2 answers

Laravel Eloquent find posts that has all the given tags

Consider 3 tables as below class Post extends Model { public function tags() { return $this->belongsToMany(Tag::class, 'post_tags', 'post_id', 'tag_id'); } } posts table |id | title | +---+---------+ |1 | post1 | |2 | …
Hadi Sharghi
  • 903
  • 16
  • 33
3
votes
3 answers

HABTM 2 tables 2 different relationships

I have a Service Types table containing id and name of a couple of dozen services. I have a Projects table that has to have a list of Proposed Services, and a list of Accepted Services. I know that I would use HABTM on both sides with a…
anithri
  • 340
  • 1
  • 9
3
votes
1 answer

to_xml Doesn't Work on Objects Returned Through Rails ActiveRecord habtm Reference

I have two rails active record classes, School and Instructor linked by a has_and_belongs_to_many relationship. I need to query my instructors_controller for instructors for a particular school and return an xml format response. Therefore, in the…
Colin
  • 2,089
  • 25
  • 34
3
votes
2 answers

accpets_nested_attributes_for, HABTM, and find_or_create

I'm using Rails' accepts_nested_attributes_for method with great success, but how can I have it not create new records if a record already exists? I have found this article, accepts_nested_attributes_for with find_or_create? but it doesn't seem to…
3
votes
2 answers

HABTM best practice

I have 2 main entities, UserProfile and Property. Basically, the UserProfile needs to maintain 3 different lists of Properties (note, each list type will have additional properties) Does anyone see anything wrong with the following design for doing…
3
votes
1 answer

Postrgresql looking for the wrong many to many table with Rails 3

**UPDATE: I found this: "Active Record expects intermediate join tables to be named with a concatenation of the tables it joins, in alphabetical order." Sigh... ** I have 3 models: images, videos and keywords. Images and videos look basically the…
3
votes
1 answer

create specific scope HABTM Rails 6

I have a User model and a Conversation model. User has many conversations and Conversation has many users, so I use has_and_belongs_to_many relationship through join table :conversations_users. I use the method to return the conversation between…
3
votes
1 answer

Sequelize - Delete row in junction table when two tables have belongsToMany association

I have two tables in sequelize and they have belongstoMany association with each other. I have a third junction table and I want to delete cascade row from junction table when a row is deleted from two table. Sequelize version : 3.23.0 I have tried…
Payalv
  • 91
  • 1
  • 7
3
votes
2 answers

cakephp testing suite: import fixtures tables for HABTM

I have a model Post -> hasAndBelongsToMany -> Tag For testing, I created the fixtures for each model, for example, a fixture for the model Post looks like this class PostFixture extends CakeTestFixture { var $import = array('model' => 'Post',…
Mauro Zadunaisky
  • 798
  • 1
  • 11
  • 21
3
votes
1 answer

A class was passed to `:class_name` but we are expecting a string

I'm trying to create a join table called :books_users where a column in books, :claim, is a boolean, where if a person clicks the link to "review this book", the claim action in the books controller does this: def claim book =…
3
votes
3 answers

filter Sequelize belongsToMany get association table

I am working with Sequelize 4.37.10 and it works great. Unfortunately the documentation is not perfect in my opinion. So it lacks a bit of describing the belongsToMany possibilities. I have the following problem: I defined my tables like this: const…
Patcher56
  • 193
  • 2
  • 12