Reputation: 125
Let's say I currently have a table titled "Report" and within Report, there are a bunch of strings, integers, timestamps, etc.
Now, I want to somehow be able to see who is currently working on a certain report - all I need are names, but the key is it could be more than one name so just adding a string column to the Report table wouldn't work. Plus, the names would keep changing - at one point, "Steve" and "John" could be working on a report, but once "Steve" stops, I need to be able to get rid of his name from the table.
I have a feeling that the correct way to do this is by:
I think that makes sense logically, but I'm hesitant to implement this without being sure that this is the way to go. Any help/feedback/suggestions would be appreciated!
Edit:
I created a model for my join table and it auto-generated a migration and added an integer "viewer_id" and an integer "report_id" like Vimsha suggested below. However, I can't correctly implement the join tables in my code for some reason - when I want to add to the new Table (report_viewers), I say:
@viewer = ReportViewer.new
@viewer.viewer_id = get_current_user[:id]
@viewer.report_id = this_report.id
@viewer.save
However, when I access the table via
ReportViewer.where(:report_id => my_report.id).last.viewer_id.name (the viewer table has a string name)
it gives me a nil class. Do you know why?
Upvotes: 0
Views: 71
Reputation: 3120
What i usually do is use
rails generate migration addviewertoreport
and then just add the relevant commands
Upvotes: 0
Reputation: 29349
Looks like you need a join table report_viewers
report_viewers
-report_id
-viewer_id
When you detect a new viewer, add an entry with report_id and viewer_id. Remove the record if the viewer stops viewing the report.
Upvotes: 3