I am looking to share lessons between users in a rails application. User A has created some lessons and would like to share it with user B & C. User A creates a lesson, adds users B & C to the lesson and they now can see the lesson. My problem is how I can get the shared lessons to appear in users B & C shared page? Each lesson belongs to a notebook.
notebook.rb
belongs_to :user
has_many :lessons, :dependent => :destroy
lesson.rb
belongs_to :notebook
has_many :shareships
has_many :users, through: :shareships, dependent: :destroy
attr_reader :user_tokens
accepts_nested_attributes_for :shareships, :reject_if => lambda { |a| a[:user_ids].blank? }
scope :shared, lambda { where('shared_ids = ?')
user.rb
has_many :notebooks, dependent: :destroy
shareship.rb
belongs_to :lesson
belongs_to :user
lessons_controller.rb
class LessonsController < ApplicationController
before_filter :authorize, :find_notebook
load_and_authorize_resource :through => :notebook, :except => [:public]
respond_to :html, :js, :json
def create
@lesson = @notebook.lessons.build(params[:lesson])
@lesson.user_id = current_user.id
flash[:notice] = 'lesson Added!.' if @lesson.save
respond_with(@lesson, :location => notebook_lessons_path)
end
def shared
@user = current_user
@shared = @notebook.lessons
end
end
I have setup the many to many association between users and lessons so users can add other users to a lesson but I am trying to figure out how to list the lessons for the shared users. Any ideas how I can get this to work? I am having trouble setting it up and my controller and view.
I would do something like this
notebook.rb
lesson.rb
user.rb
shareship.rb
A user would have access the lesson he owns via
and he would have access to the lessons shared with him via
The user does not have straight access to the lessons he owns, he needs to go through his notebooks. But he has straight access to the shared lessons.
what do you think ?