On Sun, Feb 8, 2009 at 8:22 AM, Geekyra <[email protected]> wrote: > > Hello, can anybody help me, how to pass @current_user (generated by > session[user_id] into a model ? for example I have a relationship like > below : > > User has many Journals > User has many Categories > Journals has many Items > Items has many Categories > > > The problem is I like to sum "amount" field in Item model based on > category and user. So every user has their own category and their own > category amount. So if I want to make a method that return sum of item > amount where do I put it anyway ? On this problem I can't make through > association cause User and Item doesn't related at all, they only > related by journals data.
Assuming all your associations are setup right, you should be able to do the following. # in journals controller, this will return the current users journal def show @journal = @current_user.journals.find( params[:id] ) end # adding a new journal for the current user # example, instead of doing something like: @journal = Journal.new( params[:journal] ) if @journal.save .... You can do the following: @journal = @current_user.journals.new( params[:journal] ) if @journal.save ... By using the associations, you'll be able to add associated data to the current user without much extra work. Hope that helps! Cheers, Robby -- Robby Russell Chief Evangelist, Partner PLANET ARGON, LLC design // development // hosting w/Ruby on Rails http://www.planetargon.com/ http://www.robbyonrails.com/ aim: planetargon +1 503 445 2457 +1 877 55 ARGON [toll free] +1 815 642 4068 [fax] --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---

