1

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

  • Full Name
  • UserName
  • Avatar URL

Product Table

  • Product Name
  • Product Price
  • User ID
  • User Full Name
  • Username
  • User Avatar URL

Wishlist Table

  • Wishlist Name
  • User ID
  • User Full Name
  • Username
  • User Avatar URL

Thanks in advance for your answers !

1
  • would you post your schema.rb file as well as the 3 models? Commented Apr 22, 2013 at 15:27

1 Answer 1

2

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)
Sign up to request clarification or add additional context in comments.

1 Comment

Ok but would this method will be faster than using joins ? or is it the same regarding the performance ? Thanks,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.