[b]rails中数据库事务:[/b]
[code]def fetch_value
sql = ActiveRecord::Base.connection();
sql.execute "SET autocommit=0";
sql.begin_db_transaction
id, value =
sql.execute("SELECT id, value FROM sometable WHERE used=0 LIMIT 1 FOR UPDATE").fetch_row;
sql.update "UPDATE sometable SET used=1 WHERE id=#{id}";
sql.commit_db_transaction
value;
end
[/code]
[b]
在ActionRecord的count()上添加适当的条件[/b]
[code]blog.posts.count(:conditions => "status = 'active'")[/code]
选择特定列的数据
[code]class User < ActiveRecord::Base
def self.names
# find all records, then map name attributes to an array
find(:all, :select => "name").map(&:name)
end
end[/code]
[b]around_filter [/b] 环绕过滤器
[code]class AssetController < ActionController
around_filter (:only => :assets) do |controller,action_block|
controller.benchmark("Benchmarking...") { action_block.call }
end
...
end[/code]
[code]def fetch_value
sql = ActiveRecord::Base.connection();
sql.execute "SET autocommit=0";
sql.begin_db_transaction
id, value =
sql.execute("SELECT id, value FROM sometable WHERE used=0 LIMIT 1 FOR UPDATE").fetch_row;
sql.update "UPDATE sometable SET used=1 WHERE id=#{id}";
sql.commit_db_transaction
value;
end
[/code]
[b]
在ActionRecord的count()上添加适当的条件[/b]
[code]blog.posts.count(:conditions => "status = 'active'")[/code]
选择特定列的数据
[code]class User < ActiveRecord::Base
def self.names
# find all records, then map name attributes to an array
find(:all, :select => "name").map(&:name)
end
end[/code]
[b]around_filter [/b] 环绕过滤器
[code]class AssetController < ActionController
around_filter (:only => :assets) do |controller,action_block|
controller.benchmark("Benchmarking...") { action_block.call }
end
...
end[/code]