postgresql - Catch postgres trigger in rails controller after insert or update operation -


i have app different models. 1 of models badge model many many relation users via achievements.

in controllers have logic check if user has gained achievement , make insert in relation table (achievement). after hard coding in controller have decided take logic trigger in postgresql.

i have programed trigger teh first badge this:

def change execute <<-trigger create or replace function firstbadge() returns trigger $$ declare begin if (select count(id) user facebookid=new.facebookid)==0 , (select count(id) achievement user_id=new.id , badge_id=1)==0     insert achievement(user_id,badge_id,created_at,updated_at)values(new.id,1,now(),now()); end if; return null; end; $$ language plepgsql volatile;     drop trigger if exists add_badge_user on paper;     create trigger add_badge_user     before insert on user each row     execute procedure firstbadge(); trigger end 

and action in controller inserts new user is:

if !params[:firstname].blank? , !params[:lastname].blank?       usuario = user.new       usuario.facebookid=params[:facebookid]       usuario.facebooktoken=params[:facebooktoken]       usuario.firstname=params[:firstname]       usuario.lastname=params[:lastname]       usuario.identifier=params[:firstname]       usuario.age=params[:age]       usuario.email=params[:email]       usuario.level=0       usuario.save       json={:authorize=> true}       render :json => json, :status => 200     end 

but have little problem, need last inserted id achievement table (the relation table) in controller return in json. , have many other functions in controllers need receive updated or inserted id's.

how can catch results of triggers on postgresql , use them in controller? have make query in controllers retrieve last achievements of users instead of receiving them triggers?

thank you.

simply put: taking steps way out of rails' philosophy. rails opinionated framework , if don't things following rails way, going hurt.

first of all, accepted in rails community logic should not belong in database, should belong in application. there's lot of heated talk this, if you're going use rails, have used it, because framework designed way. instead of trigger, should have used callback in 1 of models.

other that, controller code seems heavily bloated. encouraged keep controller code extremely thin because more maintainable , coherent mvc architecture (the controller's responsibility should instantiate model objects, perform action on them, instantiate view). common practice push such logic down models.

moreover, seems don't care least naming conventions. if don't follow rails guidelines this, lose lot of magic , wonder, , make own life lot harder. example, if had followed conventions, write controller code posted way :

  usario = user.new( params[:user] )   usario.save   render json: usario, status: 200 

that's 3 lines instead of 14.

finally, of denotes lack of knowledge framework. encourage thoroughly (re-)read rails guides, , possibly 1 or 2 of many books there out there. give more precise idea of workflow , design using wonderful of tool rails.


Comments