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

Automatically adding additional Records into HABTM table in CakePhp

Iam confused about a problem. i will describe it I am using HABTM first time in cakephp,also i am not too much familiar with cakephp 2.4.6 I have MediaOrg- model, media_orgs - table name table fields- id,name public…
SibinF
  • 385
  • 1
  • 7
  • 25
0
votes
0 answers

HABTM associations with another table

I'm in trouble with a more complex situation of HABTM in Cakephp. Ina nutshel, the situation is: a have users and movies, the relationship between then is HABTM, but i also need a relationship types. USERS - HABTM - MOVIES | …
0
votes
2 answers

rails custom key in params hash

I have a model CommunityPartner with a has_and_belongs_to_many relationship with another model PartnerTag. class CommunityPartner < ActiveRecord::Base VALID_EMAIL = /\A[\w+\-.]+@{1}[a-z\d\-.]+\.[a-z]+\z/i validates(:name, presence: true,…
Hugo
  • 2,186
  • 8
  • 28
  • 44
0
votes
1 answer

Hard time creating a class scope for HABTM relation

This is my simplified situation: class Producer has_and_belongs_to_many :rules end class Rule has_and_belongs_to_many :producers end But Rule happens to follow a STI structure. So I have this: class Producer has_and_belongs_to_many :rules …
0
votes
1 answer

HABTM - add and remove buttons (on the show view)

I am displaying an invoice, to which I want to add 'fly' products. I want to do this using a drop down box for the products along with an add button. Once a product has been added to the invoice I want to be able to click a delete button to remove…
0
votes
1 answer

Correct way to link habtm objects in rails?

I have two objects: Surveys and Questions. They are set up in a HABTM relationship such that surveys can have multiple questions and questions can belong to multiple surveys. I am very new to Ruby on Rails, but have extensive experience with…
Andrew K
  • 1,571
  • 1
  • 17
  • 25
0
votes
1 answer

Creating two HABTM associated records, simultaneously

In a CakePHP, app, let's assume I have two models: Team and User. Those models are associated with each other in a HABTM relationship (joined by table teams_users). Say we have a form where someone can specify details to create a new Team and User,…
Benjamin Allison
  • 2,134
  • 3
  • 30
  • 55
0
votes
1 answer

Database field refuses to be an integer - Rails/Postgres

I am creating a join table, broadly following the Railscast: http://railscasts.com/episodes/17-habtm-checkboxes-revised?view=asciicast I cannot set the has_many records on the object and get the following error: 2.0.0p353 :012 > invoice.fly_ids …
0
votes
1 answer

CakePHP: join table is empty (HABTM)

I have the following HABTM relationship: //Message.php class Message extends AppModel { public $hasAndBelongsToMany = array( 'User' => array( 'className' => 'User', 'joinTable' => 'messages_users', 'foreignKey' =>…
MorbidDesign
  • 40
  • 12
0
votes
0 answers

association linking models together HaBtM doent function

This is my error: Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'users_groupnames.id' in 'field list' And the SQL Query: SELECT `Groupname`.`id`, `Groupname`.`GroupName`, `Groupname`.`User_id`, `UsersGroupname`.`id`,…
0
votes
1 answer

Finding if HABTM model association already exists

I have Users and Courses. They have a HABTM relationship that is manifested as Subscriptions (that's the table with user_id and course_id). I need to have a condition on the Course page that checks if the user is subscribed to this course. I am not…
itamar
  • 3,837
  • 5
  • 35
  • 60
0
votes
2 answers

Rails model design decision - trying to avoid creation of mutliple new models using HABTM x 2

I've been following the HABTM Railscast and am looking to extend it. The Railscast way is great for adding categories to a product. But lets say I have (say) 5 ways of categorising products along different dimensions (and these change…
0
votes
2 answers

Rails 4 Setting A Relationship with HABTM

I have two existing Models - Schools and Users. class School < ActiveRecord::Base has_and_belongs_to_many :users class User < ActiveRecord::Base has_and_belongs_to_many :schools My question is, how can I assign and user to a school? I can't quite…
0
votes
2 answers

CakePHP - Saving HABTM

// Models/Tables with HABTM: companies, sectors, companies_sectors // Controller $sectors = $this->Sector->find('list', array('fields'=>array('Sector.id', 'Sector.sector'))); // View echo $this->Form->input('Sector.id', array( 'multiple' =>…
0
votes
2 answers

Rails - HABTM relationship...Storing Checkbox Values from Users

Im trying to figure out this HABTM relationship issue to store in the interests of users within my database. The interests table has a list of different interests with id and name (i.e: id=1 , name = 'Music') I have a users model =>…