Reputation: 169
I got stuck with this scenario. Let me explain as follows. I have a movies model, for a movie a user can give a rating and he can comment for a movie. So I have points table. Based up on the number of comments, rating and average rating e.t.c I wrote a method def points end in my movies controller.
I have a comment controller and method in it separately for creating a comments, and for rating a movie I have written a a method in my movies controller for creating ratings. Hope Im clear till now.
so my problem statement is. I want call this points method in movies controller at the time of creation of ratings and comments.
for brief insight Im pasting my code here.
In movies controller my two methods(ratings and points)
def rate
@movie= Movie.find(params[:movie_id])
@count=0
@test= Rating.find(:last, :conditions=>['movie_id=?', @movie.id])
unless @test.nil? || @test.blank?
@count= @test.count
end
if(params[:submitted])
if @movie.ratings.create(:movie_id => params[:movie_id], :rate => params[:rating], :user_id=> current_user.id, :count=> @count+1)
points
flash[:success] = "Rating was successfully saved."
else
flash[:error] = 'Error. Rating was not saved. Try again later.'
end
redirect_to @movie
end
end
def points
@user= current_user
@movie= Movie.find(params[:movie_id])
total_comments_by_user=Comment.find(:all, :conditions=>['user_id=? AND movie_id=?',@user.id,@movie.id]).count
user_who_rated= Rating.find(:all, :conditions=>['user_id=? AND movie_id=?',@user.id,@movie.id])
@user_who_rated.each do |ur|
if @user=ur.user_id
@ratings_of_each_user=ur.rate
end
end
@over_all_comments_of_movie=Comment.find(:all, :conditions=> ['movie_id=?', @movie.id]).count
@records=Rating.find(:all, :conditions => ['movie_id=?',@movie.id]).collect(& :rate)
unless @records.nil? || @records.blank?
@average_rating = 0.0
@records.each do |r|
@average_rating += r
end
@average_rating = @average_rating/@records.size
end
unless @ratings_of_each_user.nil? || @ratings_of_each_user.blank?
if @total_comments_by_user.nil? || @total_comments_by_user.blank?
else
@div_one=@ratings_of_each_user*@total_comments_by_user
@div_two=@over_all_comments_of_movie* @average_rating
@total=0.0
@total=@div_one.to_f/@div_two.to_f
@find_user_in_points= Point.find(:all, :conditions=> ['user_id=? AND movie_id=?',current_user.id,@movie.id])
if @find_user_in_points.nil? || @find_user_in_points.blank?
Point.create(:user_id=> current_user.id, :movie_id=>@movie.id, :points=> @total)
else
@find_user_in_points.each do |f|
if f.user_id= current_user.id
f. update_attributes(:points=> @total)
end
end
end
end
end
end
Now comments controller
def create
@user=current_user
@movie = Movie.find(params[:movie_id])
@select_params= params[:choice_list] || []
if @select_params.nil? || @select_params.blank?
@comment = @movie.comments.create(params[:comment])
@comment.update_attributes(:user_id=>@user.id, :commenter => @user.username)
else
@comment= @movie.comments.create( :commenter => "user", :body=>params[:comment][:body] , :movie_id=> @movie.id, :user_id=>@user )
end
redirect_to movie_path(@movie)
end
def destroy
@comment = Comment.find(:all, :conditions=>['id=?',params[:id]])
@comment.delete
redirect_to movie_path(@movie)
end
I have rate movie link on my view file and a form to add a comment to the movie. I just want to invoke that points method when ever a rating is created and comments are created so that i can perform my points caluculation internally and update my points table.
Guide me please. Is it possible to call a method in one controller from the other controller if so how and tell me how to call a method in the same controller.
Upvotes: 0
Views: 231
Reputation: 1257
Don't call another controller. If you need to share method, either make sure its in the model (if that's the correct place for it) or in a shared controller like ApplicationController
Also...
unless @test.nil? || @test.blank?
- String#blank?
checks .nil? first. So the .nil? is not necessary.
Upvotes: 0