How do I increment or decrement an association field in ruby on rails -
some here folks: have 2 models (category, movie)that related this:
category has_many movies, movie belongs_to category
add_one belongs_to movie
add_one belongs_to :user
user has_many :add_one
movie has_many :add_one
i created table has 2 integer fields want increment , decrement depending on value of field.it has 2 other fields foreign keys. if clicked current user , value 1 should decrement 0 else 1. note: don't want use vote gem....i used on model want create have described here..any please.
create_table "add_one", force: :cascade |t| t.integer "ineedone", default: 0, null:false t.integer "ineedonetoo", default: 0, null:false t.integer "user_id" t.integer "movie_id" end routes: resources :category resources :movies member put "addone" => "movies#toaddone" end end end
in movie's show page did
link_to("add", toaddone_category_movie_path(@category, @movie, @user), method: :put)
i created movie partial _movie.html
and loop through this:
<%category.movies.each |movie|%> <%=..........%> <%movie.add_ones.each |add|%> <%=add.ineedone%> end end
the controller movie .
.....#before_action :find_category... before_action :find_movie....etc def toaddone @movie.add_one.each |add| add.increment!(:ineedone) add.create_activity :ineedone end
.....
it runs , doesn't show error in server....but unfortunately when call add_one in rails console come out empty means nothing created , _movie partial shows nothing...please out please want show first before creating decrement action.
Comments
Post a Comment