activerecord

jActiveRecord入门

别说谁变了你拦得住时间么 提交于 2020-03-01 04:52:49
jActiveRecord jActiveRecord 是我根据自己的喜好用 Java 实现的对象关系映射(ORM)库,灵感来自 Ruby on Rails 的 ActiveRecord 。它拥有以下特色: 零配置:无XML配置文件、无Annotation注解。 零依赖:不依赖任何第三方库,运行环境为Java 6或以上版本。 动态性:和其他库不同,无需为每张表定义一个相对应的静态类。表、表对象、行对象等都能动态创建和动态获取。 简化: jActiveRecord 虽是模仿 ActiveRecord ,它同时做了一些简化。例如让HasMany、HasAndBelongsToMany等关联对象职责单一化,方便理解。 项目已经托管到OSC Git: http://git.oschina.net/redraiment/jactiverecord 。欢迎大家一起参与维护。 入门 参考 Rails For Zombies ,我们一步一步创建一套僵尸微博系统的数据层。 连接数据库 jActiveRecord 的入口是 me.zzp.ar.DB 类,通过open这个静态方法创建数据库对象,open方法的参数与 java.sql.DriverManager#getConnection 兼容。 DB sqlite3 = DB.open("jdbc:sqlite::memory:"); 作为演示

脱离Rails使用ActiveRecord

坚强是说给别人听的谎言 提交于 2020-02-29 21:59:34
ActiveRecord是Ruby的对象-关系映射(ORM)框架,它几乎总是被视为Rails框架的一部分,但其自身也是一个实体,可通过gem单独安装和使用。本文以访问Sqlite3为例,介绍如何在Rails框架之外使用ActiveRecord访问数据库。 1. 安装ActiveRecord 首先通过gem安装ActiveRecord,运行以下命令: gem install activerecord #2. 安装Sqlite3 为了方便起见,本文演示访问Sqlite3数据库,访问其他数据库(例如MySQL、PostgreSQL等)方法类似。 gem install sqlite3 3. 连接数据库 require 'active_record' ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:") “:adapter”适配器一项选择相应的数据库适配器,此处是sqlite3; “:database”为目标数据库,sqlite是文件型数据库,因此这一项是目标文件的路径,例如“info.db”。“:memory:”是一个特殊的文件,使用内存而不是外部文件。 4. 建表 ActiveRecord::Schema.define do drop_table :hosts if

Codeigniter active record with several like or?

ε祈祈猫儿з 提交于 2020-02-28 08:11:29
问题 Hey.. I've run into a problem using several "like" in my sql-query (generated with Codeigniter's activerecord): SELECT * FROM (`posts`) WHERE `city` LIKE '%test%' AND `title` LIKE '%%' OR `text` LIKE '%%' Thing is, it appears to read this query as if there was a parenthesis around the first like and the second like, but I want there to be a parenthesis around the second and the last like (I want it to compare if the last or the next to last works). How can I achieve this using Codeigniter's

Codeigniter active record with several like or?

≡放荡痞女 提交于 2020-02-28 08:10:10
问题 Hey.. I've run into a problem using several "like" in my sql-query (generated with Codeigniter's activerecord): SELECT * FROM (`posts`) WHERE `city` LIKE '%test%' AND `title` LIKE '%%' OR `text` LIKE '%%' Thing is, it appears to read this query as if there was a parenthesis around the first like and the second like, but I want there to be a parenthesis around the second and the last like (I want it to compare if the last or the next to last works). How can I achieve this using Codeigniter's

条件使用NOT NIL的Rails

风格不统一 提交于 2020-02-28 04:13:27
使用rails 3样式我怎么写相反的: Foo.includes(:bar).where(:bars=>{:id=>nil}) 我想找到id不是零的地方。 我试过了: Foo.includes(:bar).where(:bars=>{:id=>!nil}).to_sql 但那回归: => "SELECT \"foos\".* FROM \"foos\" WHERE (\"bars\".\"id\" = 1)" 这绝对不是我需要的,而且几乎看起来像是ARel中的一个错误。 #1楼 对于Rails4: 所以,你想要的是一个内连接,所以你真的应该只使用连接谓词: Foo.joins(:bar) Select * from Foo Inner Join Bars ... 但是,对于记录,如果你想要一个“NOT NULL”条件,只需使用not predicate: Foo.includes(:bar).where.not(bars: {id: nil}) Select * from Foo Left Outer Join Bars on .. WHERE bars.id IS NOT NULL 请注意,此语法报告了弃用(它讨论了字符串SQL片段,但我想在解析器中将哈希条件更改为字符串?),因此请务必将引用添加到结尾: Foo.includes(:bar).where.not(bars:

我如何获得Ruby类的名称?

十年热恋 提交于 2020-02-28 03:26:35
如何从ActiveRecord对象获取类名? 我有: result = User.find(1) 我试过了: result.class # => User(id: integer, name: string ...) result.to_s # => #<User:0x3d07cdc>" 我只需要字符串中的类名(在本例中为 User )。 有没有办法呢? 我知道这是非常基本的,但我搜索了Rails和Ruby的文档,我找不到它。 #1楼 这是正确答案,摘自Daniel Rikowski和pseidemann的评论。 我厌倦了通过评论来寻找合适的答案...... 如果您使用Rails(ActiveSupport): result.class.name.demodulize 如果你使用POR(普通红宝石): result.class.name.split('::').last #2楼 在我的情况下,当我使用类似 result.class.name 东西时,我得到了类似 Module1::class_name 东西。 但是如果我们只想要 class_name ,请使用 result.class.table_name.singularize #3楼 如果要从类方法中获取类名,则 class.name 或 self.class.name 将不起作用。 这些只会输出 Class ,因为类的类是

如何在Ruby on Rails ActiveRecord迁移中处理太长的索引名?

笑着哭i 提交于 2020-02-27 14:01:30
我试图添加从四个关联表的外键创建的唯一索引: add_index :studies, ["user_id", "university_id", "subject_name_id", "subject_type_id"], :unique => true 数据库对索引名称的限制导致迁移失败。 这是错误消息: 表“研究”上的索引名称“ index_studies_on_user_id_and_university_id_and_subject_name_id_and_subject_type_id”太长; 限制为64个字符 我该如何处理? 我可以指定其他索引名称吗? #1楼 与上一个答案类似:只需在常规的add_index行中使用“名称”键: def change add_index :studies, :user_id, name: 'my_index' end #2楼 你也可以 t.index([:branch_id, :party_id], unique: true, name: 'by_branch_party') 就像 Ruby on Rails API中一样 。 #3楼 在PostgreSQL中, 默认 限制是63个字符 。 因为索引名称必须唯一,所以有一些约定是很好的。 我使用(我调整了示例以解释更复杂的结构): def change add_index :studies

Ruby的双冒号`::`是什么?

丶灬走出姿态 提交于 2020-02-27 06:31:05
这是什么双冒号 :: ? 例如 Foo::Bar 。 我找到一个 定义 : :: 是一元运算符,它允许在类或模块之外定义的任何位置访问常量,实例方法和类方法。 如果仅使用 :: 公开任何内容,范围(私有,受保护)有什么用? #1楼 Ruby on rails使用::进行名称空间解析。 class User < ActiveRecord::Base VIDES_COUNT = 10 Languages = { "English" => "en", "Spanish" => "es", "Mandarin Chinese" => "cn"} end 要使用它: User::VIDEOS_COUNT User::Languages User::Languages.values_at("Spanish") => "en" 另外,其他用法是:使用嵌套路由时 OmniauthCallbacksController 在用户下定义。 路线就像: devise_for :users, controllers: {omniauth_callbacks: "users/omniauth_callbacks"} class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController end #2楼 module

如何在ActiveRecord中设置默认值?

浪子不回头ぞ 提交于 2020-02-26 05:23:28
如何在ActiveRecord中设置默认值? 我看到Pratik的一篇文章描述了一段丑陋而复杂的代码: http : //m.onkey.org/2007/7/24/how-to-set-default-values-in-your-model class Item < ActiveRecord::Base def initialize_with_defaults(attrs = nil, &block) initialize_without_defaults(attrs) do setter = lambda { |key, value| self.send("#{key.to_s}=", value) unless !attrs.nil? && attrs.keys.map(&:to_s).include?(key.to_s) } setter.call('scheduler_type', 'hotseat') yield self if block_given? end end alias_method_chain :initialize, :defaults end 我已经看到以下示例谷歌搜索: def initialize super self.status = ACTIVE unless self.status end 和 def after_initialize

如何在Rails中重定向到404?

江枫思渺然 提交于 2020-02-26 01:42:29
我想在Rails中“伪造” 404页面。 在PHP中,我只发送带有错误代码的标头,如下所示: header("HTTP/1.0 404 Not Found"); Rails如何做到这一点? #1楼 由于错误处理程序已移至中间件,因此所选答案在Rails 3.1+中不起作用(请参阅 github issue )。 这是我很满意的解决方案。 在 ApplicationController : unless Rails.application.config.consider_all_requests_local rescue_from Exception, with: :handle_exception end def not_found raise ActionController::RoutingError.new('Not Found') end def handle_exception(exception=nil) if exception logger = Logger.new(STDOUT) logger.debug "Exception Message: #{exception.message} \n" logger.debug "Exception Class: #{exception.class} \n" logger.debug "Exception