Show value of variable from a different entry in Rails 3? -


i have nested form. form model called list, has :name , :month attribute. list model has model nested inside called item, has :name , :amount attributes. list has many items, , item belongs list. created variable in controller (not sure if that's best way) totals of item amounts this:

@amount_total = item.where(:list_id => @list).sum("amount") 

i call variable in list show page , shows total of item amounts current list id. want able compare current list total entered list. possible using variable this? if so, how? if not, should do?

edit

in show action in controller have following(based on answer below):

    @list = list.find(params[:id]) 

but need create variable @previous_list, this:

@previous_list = list.order("id").where("id<? , user_id=?", self.id, self.user_id).last 

i need find entered list same user. however, when put variable way, says there undefined method "user_id". thoughts?

you should move logic list model, , create total_amount accessor in there, like:

def total_amount   item.where(:list_id => self.id).sum("amount") end 

then in controller need load previous list @previous_list variable.

in views able do:

<%= @list.total_amount %> <%= @previous_list.total_amount %> 

ian.


Comments