This project is archived and is in readonly mode.
Accessing named_scope with :include in view
Reported by jcrip | April 10th, 2009 @ 03:04 AM | in 2.x
I'm using the USDA SR-21 Database for a calorie counting website. To get the calories/quantity I have created some named scopes in the model food.rb
named_scope :by_long_desc, lambda { |long_desc| {:conditions => ["long_desc like ?", "%#{long_desc}%"], :limit => 100 } } named_scope :by_fd_group_id, lambda { |fd_group_id| {:conditions => ["fd_group_id = ?", fd_group_id] } } named_scope :fd_nutrs, :include => :nutrs, :conditions => {'nutrs.nutr_def_id' => 208}
which work and with the controller calling
if params[:search_fd_groups][:id].blank?
@foods = Food.by_long_desc(params[:search_long_desc]).fd_nutrs
#raise @foods.to_yaml
else
@foods = Food.by_long_desc(params[:search_long_desc]).by_fd_group_id(params[:search_fd_groups][:id]).fd_nutrs
end
end
when raise raise @foods.to_yaml
I get
- !ruby/object:Food attributes: n_factor: "6.25" ref_desc: "" updated_at: com_name: "" fat_factor: "9.02" shrt_desc: BEERWURST,BEER SALAMI,PORK & BF fdgrp_cd: "0700" cho_factor: "3.68" pro_factor: "4.27" id: "7002" survey: Y manu_facname: "" refuse: "0" long_desc: Beerwurst, beer salami, pork and beef ndb_no: "07002" created_at: 2009-01-29 18:18:10 sci_name: "" fd_group_id: "700" attributes_cache: {}
nutrs: - !ruby/object:Nutr
attributes:
cc:
updated_at:
low_eb: "0.000"
std_error: "0.000"
val: "277.000"
stat_cmt: ""
df: "0"
min_val: "0.000"
add_nutr_mark: ""
num_studies: "0"
ref_ndb_no: ""
nutr_def_id: "208"
up_eb: "0.000"
id: "101976"
deriv_cd: NC
src_cd: "4"
num_data_pts: "0"
nutr_no: "208"
ndb_no: "07002"
created_at: 2009-01-29 18:13:08
max_val: "0.000"
food_id: "7002"
attributes_cache: {}
which is all good, but how do I access nutrs.val in the view.
Consumption
<%= render :partial => "search_foods" %>
Number of records returned: <%=h @foods.count %>
<%=h food.long_desc %> | <% for gram in food.grams %><% form_tag 'add_to_cart' do %><%= text_field_tag 'quantity' %><%=h gram.amount %>-<%=h gram.msre_desc %> <%= hidden_field_tag :food_id, gram.food_id %> <%= hidden_field_tag 'gram_id', gram.id %> <%= submit_tag "Add" %> | <% end %><% end %>
I need to calculate quantity.to_f*gram.gm_wgt*nutr.val/100 to show calories pre item.
I'm so close.............thanks
Comments and changes to this ticket
-
Frederick Cheung May 7th, 2009 @ 11:50 PM
- State changed from new to invalid
Sorry, this isn't a support forum for rails. You might want to try the rubyonrails-talk google group or the rubyonrals channel on freenode.
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>