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

belongsTo() not returning relationships for User

I already tried several things but I can't get this to work. I want to be able to make something like this {{ $user->city->name }} My user model:
noeyeat
  • 325
  • 1
  • 5
  • 21
0
votes
1 answer

How to get has_and_belongs_to_many relation objects together

I have HABTM associated tables like below. class User < ActiveRecord::Base has_and_belongs_to_many :groups end class Group < ActiveRecord::Base has_and_belongs_to_many :users end How do I get User(s) with associated Groups…
Bigair
  • 1,452
  • 3
  • 15
  • 42
0
votes
1 answer

has_and_belongs_to_many association that acts as "through"

MODELS Patient: Description: Should have only 1 Referrer Association: has_and_belongs_to_many :referrer Referrer: Description: Contains all the types of referrers such as, but is not limited to: Partners etc. Association:…
0
votes
1 answer

validates_associated and HABTM

Article HABTM authors In Article model, i say: validates_associated :authors But, by creating of the new Article, this validation does't happen, because i don't see the errors. Another errors are displayed properly, but this. I render errors…
AntonAL
  • 16,692
  • 21
  • 80
  • 114
0
votes
1 answer

Rails - What are the proper 3-way table relationships?

I am working on an application wherein I am stuck deciding on a relationship. The situation is this: The implemented relationships are: Category has_many Items Outlets HABTM Categories The problem is that Outlets needs to have many-to-many…
0
votes
1 answer

ActiveAdmin has_many through - undefined method `klass' for nil:NilClass

Trying to figure out why ActiveAdmin is breaking on me, your help is appreciated. EDIT: Turns out I ran myself into a bit of an x-y problem situation with this. I apologize to the SO community... I assumed the has_many :venue_rps relationship was…
0
votes
1 answer

Join table HABTM association

I have created a join table for categories and products. Do I need a category_id column in my products database? When I go into my rails console and type Product.new there is no category attribute. If I would like to assign categories to a product…
Kris
  • 187
  • 1
  • 2
  • 14
0
votes
1 answer

CakePHP 3 - How to validate multiple select field (belongsToMany Association)

How to validate in CakePHP 3 multiple select field (belongsToMany Association)? All I need is validation rule. Multiple select field:
user3661042
  • 167
  • 1
  • 13
0
votes
1 answer

How to display results from a HABTM relationship in a view?

I have a HABTM relationship between a books table and an authors table, joined with an authors_books table. However, I can't find a solution to display the fields firstname, lastname and fullname in my view. book-model: class Book extends AppModel…
Lars
  • 1
0
votes
1 answer

inserting data through HABTM

I have a HABTM association between Users and Workspaces, which also has groups. The problem is I can't insert data intro groups from users. class User < ActiveRecord::Base has_and_belongs_to_many :workspaces #, dependent: :destroy has_many…
Gaston
  • 1,004
  • 1
  • 9
  • 23
0
votes
4 answers

CakePHP - Calling a model function in a HABTM

I have two tables, Pages and Posts that are in HABTM and are joined using pages_posts join table. My Page model along with the HABTM definition contains a function.. class Page extends AppModel { var $name = "Page"; ...... ...... …
Ashok
  • 1,074
  • 3
  • 14
  • 24
0
votes
0 answers

Adding a second-order join table in CakePHP

I'm upgrading a 2-year-old project and need to create more complex model relationships; I'm a little out of my depth. I had these tables: items addons orders items_addons Now I've added these tables: orders_items orders_items_addons I am getting…
Jonline
  • 1,677
  • 2
  • 22
  • 53
0
votes
0 answers

CakePHP 2.x get data from HABTM table

I have 3 models in my CakePHP application that are linked with a HABTM association following the CakePHP naming conventions: posts - post_tag_links - tags In my front-end postController.php file, I want to get query all the posts with their related…
Sam
  • 472
  • 1
  • 8
  • 19
0
votes
1 answer

rails validation on accepts_nested_attributes_for and habtm association in an update resets the association

In rails 2.3.8 I am having trouble with validations on a accepts_nested_attributes_for association if I loop through or even inspect the habtm association in validation the lessons are loaded and any updates are lost. For example, I have the…
0
votes
0 answers

Error on association belongsToMany when I try to persist It

When I tried to associate the model 'Media' with model 'Product' in a relationship 'many to many' through table 'media_products'. I can rescue all values associated through methods 'get' and 'find' of class 'ProductsTable' once It was directly…
user5020
  • 111
  • 5