ruby Active Record代码示例

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby Active Record代码示例相关的知识,希望对你有一定的参考价值。


ActiveRecord::Base.transaction do
  1000.times { Model.create(options) }
end

1000.times do |i|
  Foo.connection.execute "INSERT INTO foos (counter) values (#{i})"
end

Foo.transaction do
  1000.times do |i|
    Foo.connection.execute "INSERT INTO foos (counter) values (#{i})"
  end
end

ActiveRecord::Base.establish_connection
ActiveRecord::Base.connection.execute(sql,[num1,num2])

ActiveRecord::Base.connection.execute "insert into additional_news@pl select * from additional_news where id=#{self.id}"
ActiveRecord::Base.connection.execute("DELETE FROM rabbit_costs WHERE `year`=#{cost_date[0]} and `month`=#{cost_date[1]}")
# 执行存储过程
ActiveRecord::Base.connection.execute("call insert_rabbitcost(#{cost_date[0]},#{cost_date[1]},'#{c.from_date}','#{c.to_date}')")

ActiveRecord::Base.connection.execute("update department_costs set lims_cost=0, lims_labour_cost=0,lims_material_cost=0,lims_other_cost=0 WHERE `year`=#{cost_date[0]} and `month`=#{cost_date[1]}")


ActiveRecord::Base.connection.select_all(sql)

ActiveRecord::Base.connection.select_all("SELECT rabbits.farm_id,rabbits.`name` as rabbit_name,projects.`name` as project_name,projects.project_category as project_category,rabbit_costs.`year`,rabbit_costs.`month`,rabbit_handlings.handling_date as injection1_date,rabbits.died_date,
rabbit_costs.total_cost as handling_cost,rabbit_costs.feeding_days,rabbit_costs.feeding_cost
FROM rabbit_costs,rabbits,rabbit_handlings,projects
WHERE rabbit_costs.rabbit_id=rabbits.id and rabbit_costs.rabbit_id=rabbit_handlings.rabbit_id and rabbit_handlings.project_id=projects.id
and rabbit_handlings.handling_type='injection1' #{sql} ")


# 获取数据库里所有的表
ActiveRecord::Base.connection.tables

以上是关于ruby Active Record代码示例的主要内容,如果未能解决你的问题,请参考以下文章

Ruby Active Record加入并包括为count生成不同的sql

Ruby on Rails 变量在视图中显示 Active Record 数据而不是整数

ruby 使用多态数据库模型自动创建Active Record关联:http://api.rubyonrails.org/classes/ActiveRecord/Associat

Ruby on Rails。如何在 :belongs to 关系中使用 Active Record .build 方法?

在 Ruby on Rails 4 中使用 Active Record 或 Squeel Gem 重写 SQL 查询

ruby Active Model代码示例