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

How do I clean up my join_table and remove duplicate entries?

I have 2 models - Question and Tag - which have a HABTM between them, and they share a join table questions_tags. Feast your eyes on this badboy: 1.9.3p392 :011 > Question.count (852.1ms) SELECT COUNT(*) FROM "questions" => 417 1.9.3p392 :012…
0
votes
1 answer

How do I query both sides of a HABTM relationship?

I have two models - Question & Tag. Both have a HABTM association between themselves. There is also a questions_tags join table. If I have a question (q), I can find the tags by simply doing q.tags. But, if I have a tag (t), when I try to go the…
marcamillion
  • 32,933
  • 55
  • 189
  • 380
0
votes
1 answer

cakePHP saving entries to join table of existing records

first a short description. I have to models: accounts and users and a join table of it accounts_users. the models have a habtm associasions on each model: User Model: 'Account' => array( 'className' => 'Account', 'joinTable' =>…
Florin P.
  • 185
  • 1
  • 1
  • 12
0
votes
1 answer

Why I can't see the result of HABTM?

I am using cakephp since few months, and I have a problem today that I don't know why it is not working as always. I've got three table: posts comments posts_comments In the models, I set : Comment Model: var $hasAndBelongsToMany =…
NH3
  • 189
  • 2
  • 12
0
votes
1 answer

Why aren't the methods generated by has_and_belongs_to_many bidirectional?

I have... app/models/report.rb: has_and_belongs_to :standards app/models/standard.rb: has_and_belongs_to :reports db/schema.rb: create_table "reports_standards", :id => false, :force => true do |t| t.integer "report_id" t.integer…
steven_noble
  • 4,133
  • 10
  • 44
  • 77
0
votes
1 answer

Add multiple instances of HABTM associations in simple-form

I have a rails project with Products and Orders in a HABTM relationship. In the form for orders, I want a user to be able to click on a button and have it preselect a number of products (some of them in more than one quantity). I've tried using…
maschwenk
  • 569
  • 1
  • 9
  • 20
0
votes
0 answers

Get unique ids from 2 has_many relationships in rails

I can't find a simple way to get the ids of pointfields from this relationship pointtype has_and_belongs_to_many pointfields pointfield has_and_belongs_to_many pointtypes I do the following : @pointtypes = current_project.points.map{|p|…
Stéphane V
  • 1,094
  • 2
  • 11
  • 25
0
votes
1 answer

How to manage n:n relationships with Rails?

Let's say I have a Company that has many Employees and each Employee can have many Companies. Basically I will have : class Employee < ActiveRecord::Base has_and_belongs_to_many :companies end and class Company < ActiveRecord::Base …
marcgg
  • 65,020
  • 52
  • 178
  • 231
0
votes
1 answer

Tag-like skills in a user profile with a HABTM relationship

In the web application I'm building a user has a profile where they can list their skills. I would like the kind of functionality StackOverflow has when making posts, where you can type tags into the tag input and select ones that already exist, and…
James Dawson
  • 5,309
  • 20
  • 72
  • 126
0
votes
2 answers

form for object in two HABTM relationships, Rails 3

I'm trying to learn Ruby on Rails by building a website. Users can add content: Posts and Pictures. Posts and Pictures can have one or more Tags. I want each relationship to be HABTM so I can easily access posts, pictures via specific tags, and…
0
votes
1 answer

Rails Relationship: Referencing the same table twice within a many-to-many relationship

I'm pretty new to rails and trying to figure out relationships. The data I'm trying to model is as follows: An Event is a gathering where people play games. An Event is recorded in multiple Videos. A Video contains multiple GameSets. A GameSet has…
0
votes
1 answer

How can I create a drop down for a has_and_belongs_to_many relationship?

Here are my models: class ThesisGroup < ActiveRecord::Base belongs_to :course has_and_belongs_to_many :students attr_accessible :code, :title, :course_id end class Student < ActiveRecord::Base has_and_belongs_to_many :thesis_groups …
sergserg
  • 21,716
  • 41
  • 129
  • 182
0
votes
1 answer

Make CakePHP save multiple HABTM records of the same linked property

Sorry for the bad title, I have no idea how to call it otherwise. My "Product" table has a HABTM-link set up with "ProductProperty". Now, unfortunately, the client has decided every property could be added multiple times with different data. So, I'm…
Jelle De Loecker
  • 20,999
  • 27
  • 100
  • 142
0
votes
1 answer

HABTM Checkboxes using simpleform gem

I have three models, and i am trying to save onto a third table using simple form gem and checkboxes. class Work < ActiveRecord::Base has_many :skills, through: :skillships end The second model is class Skill < ActiveRecord::Base has_many…
Benjamin
  • 2,108
  • 2
  • 26
  • 46
0
votes
1 answer

Get related object has_and_belongs_to_many in rails

Let's say I got tasks and lists with an has_and_belongs_to_many relationship: class Task < ActiveRecord::Base attr_accessible :content, :due_date has_and_belongs_to_many :lists end class List < ActiveRecord::Base attr_accessible :title,…
Andre Zimpel
  • 2,323
  • 4
  • 27
  • 42