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

Cakephp order by count of join results including not existing entries

my problem is, that i want to count the votes for a comment of an article. So users can upvote good comments of an article and i want to list the comments with the most votes first. The approach that i'm following now is working, with the…
tobysas
  • 308
  • 5
  • 18
0
votes
1 answer

Validation to allow_blank unless a certain relation is selected

I have a rails 4 app with a Post model and a Tag model. Those models are related by an HABTM relation. class Post < ActiveRecord::Base has_and_belongs_to_many :tags ... end The Post model has an "image" column and validates its correctness trough…
0
votes
2 answers

rails: has_and_belongs_to_many not saving associated objects (null in join table)

Rails 4. I got an Authentication model class Authentication < ActiveRecord::Base ... has_and_belongs_to_many :posting_groups ... end and a PostingGroup model class PostingGroup < ActiveRecord::Base ... has_and_belongs_to_many…
0
votes
1 answer

achieve habtm on single model in rails

I have a problem with habtm on a single model in rails. Example: Let us say i have a User model with two roles "Student" and "teacher". User model is common for two roles. Now Each student can be associated to many teachers Each teacher can be…
aashish
  • 2,453
  • 1
  • 21
  • 19
0
votes
1 answer

Complex Comments, Tags, Votes, Favorites database relationships using Laravel

I am trying to learn HABTM and Polymorphic Relationships by building a small but complex application in Laravel 4.2. It takes in links and lets users interact with the content and each other via keywords like tag, comment, vote, favorite, and…
Qwiso
  • 135
  • 2
  • 3
  • 9
0
votes
1 answer

CakePhp condition with related HABTM model

I've got the two Models: Article and Hashtag with a HABTM relation. So in my database there are the tables articles (id,text,etc.), hashtags (id,value) and the join table articles_hashtags (article_id,hashtag_id). Now I want to find all articles…
0
votes
1 answer

Passing foreign key automatically from view to controller using HABTM models

I have two models, Companies and Employees, with a many-to-many association between them. class Company < ActiveRecord::Base has_and_belongs_to_many :employees end class Employee < ActiveRecord::Base has_and_belongs_to_many…
0
votes
1 answer

HABTM special join

ive got the following habtm relationship. My model tag is associated with many taggroups. So i can have multiple taggroups which include the same tag. I have taggroup user views, which show all including tags of this taggroup. Now there is a…
pyran1208
  • 115
  • 2
  • 11
0
votes
1 answer

Matching up collection_select output with strong parameters

In rails console & using the models below, I connected grades K, 1, and 2 to the school whose Edit form has this select field: As you can see, that association correctly selects the 3 items in the field, but if I click to select/deselect grades,…
0
votes
1 answer

created field in cakephp join table

I have the following in $this->request->data in a form post: Array ( [Customer] => Array ( [first_name] => Test [last_name] => test [dob] => Array ( [month] => 11 …
khany
  • 1,167
  • 15
  • 33
0
votes
1 answer

Rails HABTM Scoping to current subdomain

I have the following associations: class User < ActiveRecord::Base has_and_belongs_to_many :brands, :join_table => 'brands_users' has_and_belongs_to_many :companies, :join_table => 'companies_users' end class Brand < ActiveRecord::Base …
Rabbott
  • 4,282
  • 1
  • 30
  • 53
0
votes
1 answer

Cakephp HABTM same model

I'm trying to figure out how to track the owners of real property. Investors specifically. (I did spend hours researching and trying different things) I'm using HABTM on the same model. (see below for why) The difficulty I'm having is on the…
Steve Peterson
  • 132
  • 1
  • 4
  • 16
0
votes
1 answer

CakePHP 2.5 habtm does not save

I know there is a lot of questions with this specific problem and there are examples in the official documentation. But I could not find out what I am doing wrong. I have two Model with a HABTM relation, but apparently it is not saving anything in…
Tzaoh
  • 74
  • 6
0
votes
1 answer

has_and_belongs_to_many and has_many_through between the same models?

I have a model person and a model group. There are two kinds of persons: leaders lead a group and participants participate. I need a hbtm-relationship between leaders and groups and a has_many-relationship between participants and groups. Is it…
ewi
  • 175
  • 1
  • 9
0
votes
1 answer

Rails 3 has_and_belongs_to_many association: how to assign related objects without saving them to the database

Working with an has_and_belongs_to_many_association class Category has_and_belongs_to_many :projects end I would would like to use a before_filter to set projects before saving categories before_filter :set_projects, :only => [:create,…
Nanego
  • 1,890
  • 16
  • 30