我在模型中有has_many关联的表'orders'和'items'.
class Order < ActiveRecord::Base
has_many :items
class Item < ActiveRecord::Base
belongs_to :order
Run Code Online (Sandbox Code Playgroud)
项目由"数量"字段组成,订单由"quantity_sum"字段组成,用于跟踪关联项目数量的总和.
例如:
Order 1 : name='Toms shopping cart', quantity_sum=12
Item 1 : name='T-shirt', quantity=10
Itme 2 : name='Shoes', quantity=2
Run Code Online (Sandbox Code Playgroud)
我一直在寻找一种方法,以便每当添加/编辑/删除新项目时,Order的字段'quantity_sum'会自动更新.目前我一直在Item中使用after_save方法来更新Order的'quantity_sum'字段.
除了'after_save'之外还有其他任何简洁的方法吗?
与用于跟踪关联计数的"counter_cache"类似,rails是否支持自动跟踪关联中某些字段的总和?
谢谢
我一直在尝试使用RVM安装Ruby 1.9.2-head,但一直收到此错误消息:
echo executable host ruby is required
Run Code Online (Sandbox Code Playgroud)
为了通过RVM安装Ruby,是否必须拥有系统Ruby?
我有所有的依赖项rvm notes,但我没有安装任何系统Ruby.错误日志显示:
[2011-05-02 07:42:19] make gcc -O3 -ggdb -Wextra -Wno-unused-parameter -Wno-parentheses -Wpointer-arith -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -fPIC -I. -I.ext/include/i686-linux -I./include -I. -DRUBY_EXPORT -o main.o -c main.c gcc -O3 -ggdb -Wextra -Wno-unused-parameter -Wno-parentheses -Wpointer-arith -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -fPIC -I. -I.ext/include/i686-linux -I./include -I. -DRUBY_EXPORT -o dln.o -c dln.c gcc -O3 -ggdb -Wextra -Wno-unused-parameter -Wno-parentheses -Wpointer-arith -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -fPIC -I. -I.ext/include/i686-linux -I./include -I. -DRUBY_EXPORT -o dmydln.o -c dmydln.c gcc -O3 -ggdb -Wextra …
我一直试图根据我的rails3应用程序中的某个范围急切加载关联,但找不到任何解决方案.
我的应用有以下型号:
class Project
has_many :entries
has_many :to_dos
class ToDo
has_may :entries
has_many :tasks
belongs_to :project
class Task
has_many :entries
belongs_to :to_do
class Entry
belongs_to :project
belongs_to :to_do
belongs_to :task
# options format: {:from_date=>(Date.today-1.week), :to_date=>(Date.today+1.week), :user_id=>60}
scope :filtered_list, lambda { |options|
condition = options[:user_id].nil? ? "true" : "user_id = #{options[:user_id]}"
condition += options[:from_date].nil? ? "" : " AND entry_date >= '#{options[:from_date]}'"
condition += options[:to_date].nil? ? "" : " AND entry_date <= '#{options[:to_date]}'"
where(condition)
}
Run Code Online (Sandbox Code Playgroud)
在项目#index中,我有以下代码来获取用户的所有项目:
@projects = current_user.projects.includes(:entries, :to_dos =>[:entries, …Run Code Online (Sandbox Code Playgroud)