Relationship has_and_belongs_to_many Rails 4

1

I have an Item has_and_belongs_to_many template: gender and a Gender template has_and_belongs_to_many : items, also a relationship table generos_itens (id_item, genero_id).

I'm trying to create a new relationship between item and gender in the view of items (show / 1), first I would search all the genres and then with that data would include this new relationship, but since I do not have one template for the generos_itens table and also do not have the controller I do not know how to insert this record, what is the best way to proceed?

    
asked by anonymous 19.05.2014 / 21:11

1 answer

1

One way to do this would be to generate a model (not just the table) for this many-to-many relationship.

This template would contain the id's of the two related tables. (Hint: Also create a unique index for these two columns.)

And then you could create a driver for this template to work on.

But if you do not want to do this the way to create relationships is like this:

@Item.generos = @array_de_generos

So you're going to have to search all genres for the form for the user to add / delete the ones he wants, and via controller you will do something similar to the above line of code to recreate the relationships of that model. >

In this case you do not have the save method, the top row recreates the relationships.

View the documentation .

See this StackOverflow-EN question .

    
19.05.2014 / 21:33