I have a model (share) in which I save who can edit a lesson.
It is separated from the user model, and from the lesson model.
The data is currently saved as
|share_id | lesson_id | shared_ids |
| 2 | 23 | "45,66,21" |
Where “shared_ids” are the user ids that can edit the lesson
I would like to split it into
|share_id | lesson_id | shared_ids |
| 2 | 23 | "45" |
| 3 | 23 | "66" |
| 4 | 23 | "21" |
I have managed to split the shared_ids, but it is currently saved as one array inside the table :
|share_id | lesson_id | shared_ids |
| 2 | 23 | "45","66","21" |
Thanks for your help.
—- Edit —-
The Share class looks like that:
class Share < ActiveRecord::Base
attr_accessible :shared_token, :shared_ids
belongs_to :lesson
attr_reader :shared_token
serialize :shared_ids
# @Todo: save each seperately #
def shared_token=(ids)
self.shared_ids = ids.split(",")
end
end
—- Edit 2 —–
The form for shared_token in the lesson view:
<%= f.fields_for :shares do |builder| %>
<%= builder.label :shared_token, "Type the user names you wish to share the lesson with:" %>
<%= builder.text_field :shared_token, "data-pre" => @shared_ids %>
<% end %>
Since Shares are children of Lessons, I think you might want to write a methed in your Lesson model that would iterate through the ids in a list (which you’ve already figured out how to generate) and create a Share for each id. something like…
Then call that from your Lesson controller, and point your form to the appropriate controller action.