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. --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Are you sure you are trying to calculate the totals in the right place? I think the clue may be in the question. You say "every user has their own category and their own category amount" which suggests that the user owns the amounts, so possibly it should be calculated in the user model, where you can get at the users categories and journals and hence the items. 2009/2/8 Geekyra <gkrdvl-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>> > 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. > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Sun, Feb 8, 2009 at 8:22 AM, Geekyra <gkrdvl-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Many many thanks Colin i''d never figured it out before to put the calculation in the user model. My previous solution is to class Item < ActiveRecord::Bas def self.balance_per_mutation(category_id, user) Item.sum :amount, :include => :journal, :conditions => ["journals.user_id = ? and items.category_id = ?", user, category_id] end end And I find the solution is very troublesome cause everytime the amount data i need to pass current_user. And forgive me If I''m greedy but i have another problem and actually i think the same problem. The problem is for autocomplete plugin.. I''m already follow the guide on Ryan Railscasts for autocomplete association but the guide assumption is the category autocomplete is not related at all with user model, so the autocomplete for category is public category. Now my problem is i want to make autocomplete for category that display only user related category not combining with other user category data. In the comment section of Ryan solution is to make a second virtual att and model callback. I never manage to make it cos the parameter i need to pass is the current_user and really doesn''t have any clue at all, but I think the solution is the same as before maybe if i pushed the process into user model, i will achieved it, hmmmm.... wanna try out... I will post if i succeded. Anw thanks Colin On Feb 8, 11:42 pm, Colin Law <clan...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> Are you sure you are trying to calculate the totals in the right place? I > think the clue may be in the question. You say "every user has their own > category and their own > category amount" which suggests that the user owns the amounts, so possibly > it should be calculated in the user model, where you can get at the users > categories and journals and hence the items. > > 2009/2/8 Geekyra <gkr...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > > > > 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.--~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
What do u mean? Blog: http://random8.zenunit.com/ Learn rails: http://sensei.zenunit.com/ On 09/02/2009, at 3:22 AM, Geekyra <gkrdvl-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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. > > > >--~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Geekyra, Interesting.. had the same issue. Put this in application.rb around_filter :you_dont_have_bloody_clue protected def you_dont_have_bloody_clue klasses = [ActiveRecord::Base, ActiveRecord::Base.class] methods = ["session", "cookies", "params", "request"] methods.each do |shenanigan| oops = instance_variable_get(:"@_#{shenanigan}") klasses.each do |klass| klass.send(:define_method, shenanigan, proc { oops }) end end yield methods.each do |shenanigan| klasses.each do |klass| klass.send :remove_method, shenanigan end end end --courtesy Pratik Naik (though highly NOT recommended by him) works like a charm. On Feb 9, 9:26 am, Julian Leviston <jul...-AfxEtdRqmE/tt0EhB6fy4g@public.gmane.org> wrote:> What do u mean? > > Blog:http://random8.zenunit.com/ > Learn rails:http://sensei.zenunit.com/ > > On 09/02/2009, at 3:22 AM, Geekyra <gkr...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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.--~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Very interesting I will try it right away (actually I never done anything to application.rb) and the way u treat it I think it will work, thanks. On Feb 9, 5:16 pm, Ram <yourstruly.vi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Geekyra, > > Interesting.. had the same issue. Put this in application.rb > > around_filter :you_dont_have_bloody_clue > > protected > > def you_dont_have_bloody_clue > klasses = [ActiveRecord::Base, ActiveRecord::Base.class] > methods = ["session", "cookies", "params", "request"] > > methods.each do |shenanigan| > oops = instance_variable_get(:"@_#{shenanigan}") > > klasses.each do |klass| > klass.send(:define_method, shenanigan, proc { oops }) > end > end > > yield > > methods.each do |shenanigan| > klasses.each do |klass| > klass.send :remove_method, shenanigan > end > end > > end > > --courtesy Pratik Naik (though highly NOT recommended by him) > > works like a charm. > > On Feb 9, 9:26 am, Julian Leviston <jul...-AfxEtdRqmE/tt0EhB6fy4g@public.gmane.org> wrote: > > > What do u mean? > > > Blog:http://random8.zenunit.com/ > > Learn rails:http://sensei.zenunit.com/ > > > On 09/02/2009, at 3:22 AM, Geekyra <gkr...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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.--~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---