I''m trying to find a solution for the following problem: I have to list 3 things - companies'' names, total amount each company owes, total number of containers that belong to each company - using given invoices that still have a balance (they are outstanding). All this in a given period of time (filter). An invoice looks like this: class Invoice < ActiveRecord::Base has_many :line_items, :dependent => :destroy belongs_to :company and it has company_id, amount and balance. An invoice has 1 or many line items, each line item stands for a container: class LineItem < ActiveRecord::Base belongs_to :invoice belongs_to :container class Container < ActiveRecord::Base has_many :line_items Fetching the company''s name and calculating the total amount owed per company has worked so far, but I''m having a problem with adding up the containers. If I could count the line items, that would result in containers'' number. To do so, I''ve created named scopes: named_scope :filter, lambda { |invoice_filter| {:conditions => invoice_filter.sql_conditions}} named_scope :outstanding, :conditions => ["invoices.balance != ?", 0] named_scope :totals, :include => :line_items, :select => ''sum(invoices.amount) as amount, company_id, count(*) as containers_count'', :joins => :line_items, :group => ''invoices.company_id'' :filter and :outstanding refer to invoices that haven''t yet been payed (so they have a balance) in a certain period of time (from, to). :totals is the one grouping the companies by id, while summarizing their invoices'' amount and counting the line items as containers_count (attr_accessor :containers_count). The controller is: @invoice_filter = InvoiceFilter.new(params[:filter]) @invoices = Invoice.filter(@invoice_filter).outstanding.totals and the view: <% for invoice in @invoices do %> <%= invoice.containers_count %> The problem is that containers_count doesn''t get a value, hence nothing is shown in the 3rd column. I''m assuming that amount works because invoice has an amount field, but it doesn''t have a containers_count field. Any idea on how the :totals named scope might be improved? Or is there another solution for this? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
I''m gonna try to be more concise: I do a find(:group => "some_column", :select => "count(*) as some_column_count"). I did declare in the model "attr_accessor :some_column_count", but the some_column_count comes up as nil, even though the SQL query yields results. I suppose the column is nil because is not a real active record attribute. I added some_column_count as a column in the table and the assign from the group find works, but I find the solutin ugly. Is there a way to add an activerecord attribute in the model behaves the same as a normal one, only that it doesn''t map to a real column? Gabi Ge wrote:> I''m trying to find a solution for the following problem: > > I have to list 3 things - companies'' names, total amount each company > owes, total number of containers that belong to each company - using > given invoices that still have a balance (they are outstanding). All > this in a given period of time (filter). > > An invoice looks like this: > > class Invoice < ActiveRecord::Base > has_many :line_items, :dependent => :destroy > belongs_to :company > > and it has company_id, amount and balance. An invoice has 1 or many line > items, each line item stands for a container: > > class LineItem < ActiveRecord::Base > belongs_to :invoice > belongs_to :container > > class Container < ActiveRecord::Base > has_many :line_items > > Fetching the company''s name and calculating the total amount owed per > company has worked so far, but I''m having a problem with adding up the > containers. If I could count the line items, that would result in > containers'' number. To do so, I''ve created named scopes: > > named_scope :filter, lambda { |invoice_filter| {:conditions => > invoice_filter.sql_conditions}} > named_scope :outstanding, :conditions => ["invoices.balance != ?", 0] > named_scope :totals, :include => :line_items, > :select => ''sum(invoices.amount) as amount, company_id, count(*) as > containers_count'', > :joins => :line_items, > :group => ''invoices.company_id'' > > :filter and :outstanding refer to invoices that haven''t yet been payed > (so they have a balance) in a certain period of time (from, to). :totals > is the one grouping the companies by id, while summarizing their > invoices'' amount and counting the line items as containers_count > (attr_accessor :containers_count). > > The controller is: > > @invoice_filter = InvoiceFilter.new(params[:filter]) > @invoices = Invoice.filter(@invoice_filter).outstanding.totals > > and the view: > > <% for invoice in @invoices do %> > <%= invoice.containers_count %> > > The problem is that containers_count doesn''t get a value, hence nothing > is shown in the 3rd column. I''m assuming that amount works because > invoice has an amount field, but it doesn''t have a containers_count > field. Any idea on how the :totals named scope might be improved? Or is > there another solution for this?-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
I found attr_internal_accessor in ActiveSupport. http://apidock.com/rails/Module/attr_internal_accessor I put in the invoice class attr_internal_accessor :containers_count Still no help:>> Invoice.find(:first, :select => ''345 as company_id'')=> #<Invoice company_id: 345>>> Invoice.find(:first, :select => ''345 as containers_count'')=> #<Invoice > I want to have containers_count from the select to be put in the attribute containers_count in the class. Gabi Ge wrote:> > I do a find(:group => "some_column", :select => "count(*) as > some_column_count"). > > > I did declare in the model "attr_accessor :some_column_count", but the > some_column_count comes up as nil, even though the SQL query yields > results. > > I suppose the column is nil because is not a real active record > attribute. I added some_column_count as a column in the table and the > assign from the group find works, but I find the solutin ugly. > > Is there a way to add an activerecord attribute in the model behaves > the same as a normal one, only that it doesn''t map to a real column? > >-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 10 Nov 2008, at 17:51, Gabi Ge <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > I found attr_internal_accessor in ActiveSupport. > http://apidock.com/rails/Module/attr_internal_accessor > > I put in the invoice class > attr_internal_accessor :containers_count > > Still no help: >>> Invoice.find(:first, :select => ''345 as company_id'') > => #<Invoice company_id: 345> >>> Invoice.find(:first, :select => ''345 as containers_count'') > => #<Invoice > >This display (ie the output of inspect) will never show attributes other the columns on the table. If you want to see whether a method is there just call it. Fred> I want to have containers_count from the select to be put in the > attribute containers_count in the class. > > Gabi Ge wrote: >> >> I do a find(:group => "some_column", :select => "count(*) as >> some_column_count"). >> >> >> I did declare in the model "attr_accessor :some_column_count", but >> the >> some_column_count comes up as nil, even though the SQL query yields >> results. >> >> I suppose the column is nil because is not a real active record >> attribute. I added some_column_count as a column in the table and the >> assign from the group find works, but I find the solutin ugly. >> >> Is there a way to add an activerecord attribute in the model behaves >> the same as a normal one, only that it doesn''t map to a real column? >> >> > -- > Posted via http://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung wrote:> On 10 Nov 2008, at 17:51, Gabi Ge <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: > >>>> Invoice.find(:first, :select => ''345 as containers_count'') >> => #<Invoice > >> > This display (ie the output of inspect) will never show attributes > other the columns on the table. If you want to see whether a method is > there just call it.It makes no difference:>> Invoice.find(:first, :select => ''345 as containers_count'').containers_count=> nil>> Invoice.find(:first, :select => ''345 as id'').id=> 345 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 Nov 10, 6:33 pm, Gabi Ge <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Frederick Cheung wrote: > > On 10 Nov 2008, at 17:51, Gabi Ge <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > > wrote: > > >>>> Invoice.find(:first, :select => ''345 as containers_count'') > >> => #<Invoice > > > > This display (ie the output of inspect) will never show attributes > > other the columns on the table. If you want to see whether a method is > > there just call it. > > It makes no difference:>> Invoice.find(:first, :select => ''345 as containers_count'').containers_count > => nilIf you''ve still got an attr_accessor for containers_count or anything like that, this will stop the above from working. Fred> >> Invoice.find(:first, :select => ''345 as id'').id > > => 345 > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung wrote:> On Nov 10, 6:33�pm, Gabi Ge <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: >> >> It makes no difference:>> Invoice.find(:first, :select => ''345 as containers_count'').containers_count >> => nil > If you''ve still got an attr_accessor for containers_count or anything > like that, this will stop the above from working.Thanks a lot! It works. Really amazing, I think that attr_accessor was probably overwriting the actual methods what included write_attribute and read_attribute. Thank you again! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---