Ruby on Rails Monday, May 29, 2017

> On May 29, 2017, at 1:34 PM, Joe Guerra <jguerra@jginfosys.com> wrote:
>
> Hi, I have this notifications table which basically has a userid and a msg (notification).
>
> What should I do with the userid? I would like to look up the user by name and stick the id in the table.
>
> Is that normally what you'd do in this case? A dropdown list would be too large.
>
> Thanks,
> Joe

When (how often) would you want to get the user's name? That's the question I would ask before deciding whether to denormalize this or not.

If you're just doing a listing on a table, you could do Notification.includes(:user).where ... and that would lazy-load the users for you, so you could get the user's name without an N+1 lookup. If you are showing the user all of their notifications, then you would be doing something like current_user.notifications ... so you would have the user already that way.

The answer to your question depends on the context.

Walter

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/C910C1AA-AECA-4A2E-844A-C116B88A8796%40wdstudio.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment