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

How to include associated records when using to_json in rails?

I am new to rails. I have two models users and languages and i have list of languages in language model. class User < ActiveRecord::Base has_and_belongs_to_many :languages and class Language < ActiveRecord::Base has_and_belongs_to_many :users I…
logesh
  • 2,572
  • 4
  • 33
  • 60
0
votes
1 answer

Rails, has_and_belongs_to_many, :HashWithIndifferentAccess error

On my server, I have two models: Broadcast class Broadcast < ActiveRecord::Base validates_presence_of :content belongs_to :user has_and_belongs_to_many :feeds attr_accessible :content, :feeds, :feeds_attributes end Feed class Feed <…
0
votes
1 answer

Rails HABTM Relation with multiple form elements

I am working on a project that has the need for a has_and_belongs_to_many relation. This involves Users having many "proposals" and a proposal belonging to many users. This is important as a user can create a proposal, and invite other users to…
JamesWatling
  • 1,175
  • 1
  • 9
  • 17
0
votes
2 answers

HABTM Lifecycle hook

I have two models: team and season associated so that a team can belong to many seasons and each season can also have many teams. So far I have used an simple HABTM relationship between the models using a join table seasons_teams without an ID…
0
votes
1 answer

Cakephp HABTM association conditions causes Column not found SQL error

I have the problem in Cakephp that when I define public $hasAndBelongsToMany = array( 'User' => array( 'className' => 'User', 'joinTable' => 'groups_users', 'foreignKey' => 'group_id', 'associationForeignKey' =>…
Martin Cup
  • 2,399
  • 1
  • 21
  • 32
0
votes
2 answers

Rails User has many Favorite Categories. Use HABTM or no?

I have three models: User, FavCategory, and Category. Obviously the FavCategory DB table is nothing more than an association table. I currently do not have this setup as a HABTM association, because I'm only needing to reference the favorite…
Wes Foster
  • 8,770
  • 5
  • 42
  • 62
0
votes
1 answer

Rails blog and post linking

I have a rails app that works almost like a blog, and I use a tagging system to categorize the posts. I need to add to some of the posts something similar to a "related posts" feature. So for example if post 1 is related to post 4, at the end of the…
0
votes
1 answer

Couldn't find Lesson with id=0 upon save on an object with has_and_belong_to_many relationship

i have two objects - material - lesson each material can have and belong to lesson; each lesson can have and belong to material. in material_controller when i try to create @material = Material.new(params[:material]) class Material <…
0
votes
1 answer

Rails 3.2 + Postgres: has_and_belongs_to_many saving even when validation fails

I have a Company model: class Company < ActiveRecord::Base has_and_belongs_to_many :jobs validates :name, presence: true end And a Job model: class Job < ActiveRecord::Base has_and_belongs_to_many :companies validates :name,…
0
votes
1 answer

HABTM selection seemingly ignores joinTable

UPDATE #2 -- SOLUTION FOUND: Turns out my use of this lookup: $this->User->Group->find(....) was not what I needed. To pull out a user's groups I needed to use: $this->User->find('all',array('conditions' => array('User.id' => $user_id))); <…
Grambot
  • 4,370
  • 5
  • 28
  • 43
0
votes
1 answer

HABTM Just main model is saved with saveall()

I'm having problems with HABTM + cakephp. In my app i have this models (with relation definition): Cliente: public $hasAndBelongsToMany = array( 'TiposComercio' => array( 'className' => 'TiposComercio', 'joinTable' …
matheusvmbruno
  • 2,140
  • 3
  • 15
  • 20
0
votes
1 answer

CakePHP (1.3) HABTM on the same model saveAll() not working

This seems so simple but im banging my head against the wall. I have a simple HABTM relationship (Artists hasMany Artists) and im trying to save some data. I have two tables: artists: (id, name, created, modified) artists_related: (id, artist_id,…
0
votes
1 answer

How to design little complicated application in CodeIgniter?

Im writing web application based on database with many tables with many relations. For example i have table with [Items], which has many relations. Each [Item] has one [Group] Each [Item] has multiple [Ingredients] Each [Ingredient] has one…
Kamil
  • 13,363
  • 24
  • 88
  • 183
0
votes
1 answer

CakePHP: Saving HABTM relation using multiple autocomplete fields

I'm using CakePHP 2 and I have two models: Questions & Topics which are connected via a HABTM relationship. In my 'add' form for questions, I have a variable amount of autocomplete fields which should serve to couple (existing) topics to the…
0
votes
0 answers

cakephp find all posts that do not have specific habtm tag

Seems like a simple one but expected query doesn't work. The code below is just to illustrate my point. the actual model has some other conditions etc, but even when i strip them all away to something as basic as below i get no results. I've tried…
okwme
  • 740
  • 1
  • 7
  • 19