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
5
votes
2 answers

validate presence of has_and_belongs_to_many

Hi i'm using a has_and_belongs_to_many in a model. I want set the valitor of presence for kinds. and set the max number of kinds per core to 3 class Core < ActiveRecord::Base has_and_belongs_to_many :kinds, :foreign_key => 'core_id',…
Luca Romagnoli
  • 12,145
  • 30
  • 95
  • 157
5
votes
1 answer

Cakephp retrieving HABTM which Conditions

I'm using cakephp and would like to display all Submissions which are part of Category 'X' I have 4 tables with a HABTM relationship. Users -> (haveMany) -> Submissions <-> (hasAndBelongsToMany) <-> Categories but I would like to do so using the…
Mark
  • 193
  • 1
  • 2
  • 9
5
votes
1 answer

How to save in a proper way a HABTM relationship on Rails

I actually need some advice from advanced Ruby/Rails developers on what's the common and proper way to save a has_and_belongs_to_many association? Here is what I did, but to me it feels really dirty and I don't really like to see that code in my app…
lkartono
  • 2,323
  • 4
  • 29
  • 47
5
votes
1 answer

How can i use accepts nested attributes with the HABTM?

I have two models User and Category. class User < ActiveRecord::Base has_and_belongs_to_many :categories accepts_nested_attributes_for :categories end similarly class Category < ActiveRecord::Base …
5
votes
1 answer

Validating length of habtm association without saving

I have a user model with a HABTM relationship to groups. I do not want a user to be able to be in more than 5 groups, so would like to validate the length of the HABTM relationship. On the edit user page I have a list of checkboxes where the user…
jonnii
  • 28,019
  • 8
  • 80
  • 108
5
votes
3 answers

CakePHP form helper - HABTM multiple checkbox styling

I have two tables: "restaurants" and "cuisines" which are related to each other by a HABTM table The table cuisines has certain fixed entries - 54 number A restaurant can have any number of cuisines. On baking the application this came with a…
Harsha M V
  • 54,075
  • 125
  • 354
  • 529
5
votes
1 answer

Creating a new record in Rails HABTM nested form

I've got a nested form (using Ryan B's nested_form gem) using a has_and_belongs_to_many to has_and_belongs_to_many setup: Opening has_and_belongs_to_many :contacts Contact has_and_belongs_to_many :openings When trying to add a new contact to an…
5
votes
1 answer

Creating a Rails 3 HABTM with Active Admin throws a 'Can't mass-assign protected attributes:' error

I am a rails noob so the below is probably down to lack of understanding however I have been looking/reading all day and cannot seem to find the solution. I have two models - project and technology : Project : class Project < ActiveRecord::Base …
4
votes
2 answers

Ruby on Rails: Change checkbox label in formtastic

I am using formtastic to render a form for a object of a model that has a HABTM relationship with another model. I am doing this to render a list of checkboxes: <%= f.input :classes, :as => :check_boxes, :collection => UserClass.all %> And yes, it…
Hommer Smith
  • 26,772
  • 56
  • 167
  • 296
4
votes
1 answer

How do I write data to a HABTM association of a HABTM join table in CakePHP?

I'm trying to save data with following structure: As you can see, there is HABTM association between users and experiences table. And another HABTM between experiences_users and tags. I created following form:
picca
  • 857
  • 1
  • 10
  • 16
4
votes
1 answer

Using Rails' CanCan gem to handle a has_and_belongs_to_many situation

I have the following: User model that has_and_belongs_to_many Restaurants and vice-versa. Restaurant model that has_and_belongs_to_many Meals and vice-versa. In my ability.rb file, I want to specify that a user can only manage meals of restaurants…
dmonopoly
  • 3,251
  • 5
  • 34
  • 49
4
votes
2 answers

Rails not creating intermediary table? - Habtm relationship

The table between the User model and Period model does not exist. They have a has_many_and_belongs_to relationship. I'm learning Rails still, so please bear with me. This is the contents of the User.rb model file class User < ActiveRecord::Base #…
4
votes
1 answer

Has and belongs to many API updates

I have a has_and_belongs_to_many in my Rails project. The schema look like this create_table :dogs do |t| t.string :name t.string :breed t.string :dog_type end create_table :users_dogs, id: false do |t| …
Gabriel
  • 621
  • 7
  • 19
4
votes
0 answers

Sequelize belongsToMany ignores additional attributes in join table

I have two models: Article and DescriptionFragment in a BelongsToMany association through a join table Descriptions, which in turn BelongsTo another model Category and also has an attribute "sequence", all defined as follows: Article model: 'use…
4
votes
2 answers

Deleting duplicate records in join table

I have a HABTM association between user and role. User can be an admin (role_id = 1) or a user (role_id = 2) for roles. In the join table, roles_users, I have some redundant records. For ex: I want to remove the duplicate records such as 1:1,…
keruilin
  • 16,782
  • 34
  • 108
  • 175