I am building an application that requires users to schedule appointment/conversations times with mentors. I am having a hard time building this out. I have been reading up on the has_many :through => Association but I know I am going about this the wrong way.
In my User.rb
Class User < ActiveRecord::Base
has_many :mentor_requests, foreign_key: "user_id"
has_many :mentors, through: :mentor_requests
def requested?(mentor)
mentor_requests.find_by_mentor_id(mentor.id)
end
def request!(mentor_request)
mentor_requests.create!(mentor_request)
end
def unrequest!(mentor)
mentor_requests.find_by_mentor_id(mentor.id).destroy
end
end
In my Mentor.rb
class Mentor < User
has_many :mentor_requests, foreign_key: "mentor_id"
has_many :users, through: :mentor_requests
end
In the Mentor_request.rb
class MentorRequest < ActiveRecord::Base
attr_accessible :reason, :mentor_id
belongs_to :user, class_name: "User"
belongs_to :mentor, class_name: "Mentor"
validates :user_id, :mentor_id, presence: true
validates :reason, presence:true, length: { maximum: 140 }
default_scope order: 'mentor_requests.created_at DESC'
end
In my requests controller is
def create
@mentor_request = current_user.mentor_requests.build(params[:mentor_request])
#current_user.request!(@mentor)
if @mentor_request.save
flash[:success] = "Your request has been sent"
redirect_to user_path(current_user)
#Send confirmations to both user and mentor
#Send the notification to an internal message inbox
else
render "new"
end
end
When I go to the view at mentor_requests/new.html.erb and try to submit the request it says that the mentor_id has to be present and the content has to be present. I tried creating a request using a modal view from the mentor show page but the content doesnt save and I validate that the presence has to be true, and then when it redirects to mentor_requests/new.html.erb the mentor id is no longer present.
I do not know if I have presented enough information but I seriously need help here. If I am on the right path what do I need to do to get it work, and if all this is wrong what do I do to get what I want.
Thanks a lot
Jude