I'm working on a RoR applicati开发者_如何学Pythonon that deals with users creating entries. These entries have a price, a boolean which signifies the entry as either an income or an expense, and via the acts_as_taggable_on
plugin, tags. Users act_as_taggers
, so I'm able to apply ownership to tags.
I'm trying to find the users tag that has the highest sum (income entries - expense entries) as well as the tag with the lowest sum.
Right now I'm able to find all tags that the user has created:
@all_tags = current_user.owned_tags
I'm then able to go through each of the users entries, tag-by-tag, and create an array with the sums of prices for each tag:
@tag_groups = Array.new
for tag in @all_tags
entries_tagged_with_tag = current_user.entries.tagged_with(tag)
@tag_groups << entries_tagged_with_tag.sum('price')
end
This gives me the summed prices for each tag, but I'm then missing the tag name that is associated with that sum.
However, there's a pretty big catch here: An entries price by itself doesn't signify if the entry is an income or an expense. Each entry has an 'is_income' field; true meaning the entry is an income, and false means that the entry is an expense. So, the total for a tag is actually, is psuedo code:
all_incomes_with_tag(tag) - all_expenses_with_tag(tag)
The information that I'm looking to show in my view is: Tag Name, Number of Associated Entries, and Total Price. I would like to show this information for the largest expense tag, and the largest income tag.
I hope I've provided enough information. Any help, even just a point toward the right direction, would be greatly appreciated!
UPDATE
I've made some progress. Here's my current Controller:
@all_tags = current_user.owned_tags
@tag_groups = Array.new
for tag in @all_tags
income_entries_tagged_with_tag = current_user.entries.tagged_with(tag, :conditions => ['is_income = ?', true])
expense_entries_tagged_with_tag = current_user.entries.tagged_with(tag, :conditions => ['is_income = ?', false])
all_entries_tagged_with_tag = current_user.entries.tagged_with(tag)
tag_total_income = income_entries_tagged_with_tag.sum('price')
tag_total_expense = expense_entries_tagged_with_tag.sum('price')
tag_total = tag_total_income - tag_total_expense
@tag_groups << { :total_price => tag_total, :tag_name => tag.name, :number_of_entries => all_entries_tagged_with_tag.count }
end
and here's my View:
<% @tag_groups.sort_by { |tag| tag[:total_price] }.each do |tag| %>
<p><%= tag[:tag_name] %> — <%= number_to_currency(tag[:total_price]) %> (<%= tag[:number_of_entries] %>)</p>
<% end %>
Which gives me the following output:
food — $-32.50 (1)
lunch — $-32.50 (1)
coffee — $-32.50 (1)
business — $-32.50 (1)
development — $930.00 (1)
groceries — $930.00 (1)
babies — $930.00 (1)
second tag — $933.45 (1)
first tag — $933.45 (1)
personal — $1,022.00 (2)
I feel like I'm nearly there...
I am not familiar with the plugins in question. But if @tag_groups is the collection you are using to try to access the attributes you need, then all your currently doing is sending in the #sum('price') value.
You will want to make sure your collection gets all the data you want to access in the view.
@tag_groups << {"tag_name" => ..., "no_of_ass" => ..., "total" => ...}
Hash {} is always nice since it's a key:value pairs, but array [] is fine if you want to index it.
精彩评论