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

Strong params on nested attributes in rails

I'm having a problem when specifying a nested param in the strong param whitelist whitelisting with permit! works def sign_up_params params.require(:user).permit! end but specifying the param fails def sign_up_params …
user7567137
0
votes
0 answers

Setting multiple attributes in a belongsToMany relationship

I am using Sequelize ORM. how to set multiple columns in join table? for example - const UsersTasks = sequelize.define('UsersTasks', { id: { type: Sequelize.INTEGER, autoIncrement: true, primaryKey: true }, userId: { type:…
hrushilok
  • 435
  • 2
  • 5
  • 20
0
votes
1 answer

has_and_belongs_to_many issue with table_name_prefix

ich would like to ask for some help with has_and_belongs_to_many association. I have the following tables and models: candidate_job_title_translations -> Candidate::JobTitleTranslation (in a subfolder with table_name_prefix ) create_table…
0
votes
1 answer

What side do you use Uniq in rails migration

In my rails app I have 2 models Profile and Skill. Profile has_and_belongs_to_many Skill and can only have one time the same Skill. Skill has_and_belongs_to_many Profile. If we respect the first relation, it should therefore not have more than once…
0
votes
1 answer

Rails creating has_and_belongs_to_many checkbox

I'm trying to save checkbox selections in a has_and_belongs_to_many relationship. I want to link each street to the maps that it is found on. From the map table a list of checkboxes is shown on new or edit views. But I'm missing something in linking…
0
votes
2 answers

How to Deduplicate a HABTM join table in Rails

I have a join table that joins students HABTM students Unfortunately, it's gotten some duplicate records in it. How can I deduplicate the join table. Ideally, using Ruby.
Will
  • 4,498
  • 2
  • 38
  • 65
0
votes
3 answers

Rails has_many through has_and_belongs_to_many

I am trying to find way to get data from a has_and_belongs_to_many association through a secondary model. I'll say sorry now as I might not explain this right, so best I just show the associations and what I need to do. I am using Rails 5. I…
0
votes
3 answers

Rails 3 HABTM - access intersection entity

I'm attempting to approximate a polymorphic relationship in Rails 3 has-and-belongs-to-many association (HABTM). The intersection table (user_favorites) has these fields: user_id, favorite_id, favorite_class In the User class, I have the…
craig
  • 25,664
  • 27
  • 119
  • 205
0
votes
2 answers

use hasandbelongstomany model as condition

My Reservation Model hasAndBelongsToMany Profile Model. I want to find Profiles using Reservation.item_id = $id When I get the data, the data show all the datas in Profile. $profiles = $this->Profile->find('all', array( 'contain' =>…
지윤김
  • 17
  • 5
0
votes
1 answer

Query condition to HABTM association not working

I want to make a query joining two models, Devices and Users in a HABTM association: @device = Device.left_outer_joins(:users).where(users: {id: nil || 4}) I am trying to get any users that are nil in the join table or have the id 4. The problem is…
0
votes
0 answers

Get belongsToMany associations based on multiple field values

I have the following situation: Destination belongsToMany Attributes // DestinationsTable.php $this->belongsToMany('Attributes', [ 'foreignKey' => 'destination_id', 'targetForeignKey' => 'attribute_id', 'joinTable' =>…
0
votes
2 answers

HABTM echo value like standard relationship

I might be missing the point on this, but I am trying to echo out a HABTM value in my index, and I cannot seem to get the data. For example, I can echo these relationships with no issue:
OldWest
  • 2,355
  • 7
  • 41
  • 61
0
votes
1 answer

CakePHP Ignoring Containable With Pagination?

I want to have cakephp paginate data and still use containable, but for some reason it seems to ignore my pleas to contain anything and just gives me back the entire Tenant row. In my TenantsController I have the following code: $conditions =…
user559540
0
votes
1 answer

if using a join table, does the relationship have to be HABTM?

I have Worker, Manager, and Title models in Rails 2.x. There is also a JOIN table that has only worker_id, manager_id, and title_id (no explicit model for this). Because of this JOIN table (and not having a model for it), I assume I have to have the…
qali
  • 67
  • 1
  • 3
0
votes
1 answer

in Rails, using HABTM, how to avoid multiple INSERTs in join table

Using a has_and_belongs_to_many relationship, I have the following: model A has_and_belongs_to_many :B, :join_table => "A_B" has_and_belongs_to_many :C, :join_table => "A_B" table A_B A_id B_id C_id Current behavior: when doing A.save, I get 2…