Jeremy B
Jeremy B

Reputation: 911

How to update the same attributes in several tables with Postgresql and Rails

In order to minimize the number of joins that I have to execute in my application I decided to copy in my database the same field in several tables for example : I have a User, Product and a Wishlist Table.

My Product pages shows the user who created the product, as the wishlists pages which also shows the user who created them.

So I added in my products and wishlists table all the users field needed to show the required informations.

How can I update the users related fields in my Products and Wishlists Table as soon as the user change his information ?

Here is a part of my model :

User Table

Product Table

Wishlist Table

Thanks in advance for your answers !

Upvotes: 1

Views: 181

Answers (1)

Rebitzele
Rebitzele

Reputation: 3282

Firstly, by denormalizing the data in the way that you are, you are working against what relational databases are meant to do. They thrive on joins, and having each piece of data appear as few times as possible. So you really want to make sure that this is the schema that you want.

That being said, you can use the basic update_attibute syntax to update any field in any table. So, if a user edited his or her username, you would do:

User.update_attribute(:username, username)
Product.update_attribute(:username, username)
Wishlist.update_attribute(:username, username)

Upvotes: 2

Related Questions