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

multiple habtm relationships on one model

I am trying to figure out the best way to accomplish my problem. I've got a pages table, and a user_types table. I am trying to specify multiple user types on a page. They will act as permission groups. I need to do this twice however. Once for read…
Sean
  • 1,078
  • 14
  • 25
0
votes
1 answer

Need data from a many:many join in a Rails view

Its maybe not the best solution in most cases, but i want a table with data form 3 tables. class Media < ActiveRecord::Base belongs_to :user belongs_to :type has_many :ratings end class User <…
spiderbit
0
votes
1 answer

Rails can't create HABTM relationship

I've got 2 models: class Hashtag < ActiveRecord::Base has_and_belongs_to_many :photos attr_accessible :name *** end class Photo < ActiveRecord::Base has_and_belongs_to_many :hashtags, :uniq => true *** end Relationship create code: photo =…
Ivan Kozlov
  • 561
  • 2
  • 8
  • 19
0
votes
1 answer

Rails 3 HABTM Join Table Re-use With Extra Field

So I have a HABTM relationship between my Students and Classrooms using a join table called ClassroomStudents to handle many students belonging to many classrooms. A recent business requirement came up that students should be able to request…
Noz
  • 6,216
  • 3
  • 47
  • 82
0
votes
1 answer

Rails 3 : Specify inner join criteria "ON" in the rails way

I want to access the table "Words" from the "linked_to" column, and not from "id" column. The INNER JOIN query created by Rails always search for the id column in the Words table. This is what I want to do : From the Class Point, Do an inner join…
Stéphane V
  • 1,094
  • 2
  • 11
  • 25
0
votes
1 answer

rails Specify INNER JOIN column in has_and_belongs_to_many relationship

I try to link "Points" to "Words" with a has_and_belongs_to_many relationhip. I use a table named dictionnaries_points containing "dictionnary_id" and "linked_to" columns. I specified in the model the name "linked_to" of my column class Point <…
Stéphane V
  • 1,094
  • 2
  • 11
  • 25
0
votes
1 answer

How to put a self many-to-many relationship together?

Possible Duplicate: rails many to many self join I have a problem that I can't find the answer... The state is: I have a model called Accessories and I also have a relationship has_and_belongs_to_many to a table that connects 2 accessories…
0
votes
1 answer

CakePHP - should I use HABTM, and if not, how to setup a join table for multiply self-referencing model?

So I am setting up a course system, and courses have pre-reqs. One course may be a pre-req for many others and also may itself have many pre-reqs. I have a Course model, and I'm currently using a HABTM join table, because I also need to track the…
Dave Hirsch
  • 197
  • 13
0
votes
1 answer

Mechanize mass assignment error for HABTM join table

The problem is that I get this error: ActiveModel::MassAssignmentSecurity::Error: Can't mass-assign protected attributes: amenity_id when I run this code: task import_amenities: :environment do agent = Mechanize.new …
Robin Winton
  • 591
  • 10
  • 23
0
votes
1 answer

Rails Devise User both belongs_to and has_many

After spending a few days of trying to find an answer online, I figured I should ask for help. I'm trying to figure out the best way to implement these relationships using Rails Associations. I have 4 models: User, Transfer, Building and Bag. A User…
cmoel
  • 148
  • 1
  • 8
0
votes
2 answers

How do I get my create action to work for both a nested & non-nested resource?

So I have two models Topic, Client. A Client has_and_belongs_to_many :topics, and a Topic has_and_belongs_to_many :clients. Basically, what I want to happen is...when someone goes to my Topic#index, depending on how they got there (i.e. either via…
0
votes
2 answers

CakePHP - HABTM find() don't make the JOIN to other tables

My title will look like naive but I have to say I read/searched/tested everything possible, but my find() method don't implement the JOIN to related tables in the SQL query. I used it several times in other projects without problems but here... Here…
user1036495
  • 71
  • 1
  • 4
0
votes
1 answer

Select statement in a form partial for an HABTM association

I have two models Client and Topic. Both of which have a HABTM association between them. I am trying to add a select statement in my _form partial in my Client views, that allows the user to add a topic to a client (or edit that topic, etc.). This…
marcamillion
  • 32,933
  • 55
  • 189
  • 380
0
votes
1 answer

Association between models not working

I've set up a has_and_belongs_to_many association between two models. I need to access an attribute from one of the models for a method in another model. Right now my code looks like this, but I'm getting an error saying 'undefined local variable…
sacshu
  • 397
  • 9
  • 20
0
votes
1 answer

Rails HABTM without form

I have two models, Posts and Hashtags, in a HABTM relationship. My post controller looks like this: def create @post = current_user.posts.new(params[:post]) respond_to do |format| if @post.save format.html { redirect_to…
Doa
  • 1,965
  • 4
  • 19
  • 35