Tag: activerecord

Rails 3.2中具有ActiveRecord关联的无表模型

我的应用程序配置包括一些需要在AR关系中使用的值。 我知道这是一个奇怪的,可能是犯罪的尝试,但我需要将配置保持为文本文件,老实说,我认为我有一个无表格模型的好例子。 不幸的是,我无法说服AR(Rails 3.2)不要寻找表格。 我的无表款式: class Tableless < ActiveRecord::Base def self.table_name self.name.tableize end def self.columns @columns ||= []; end def self.column(name, sql_type = nil, default = nil, null = true) columns < true }.merge(opts) options[:validate] ? valid? : true end end 这是由实际模型扩展: class Stuff :stuff_things column :id, :integer column :name, :string column :value, :string def […]

启用local-infile,将数据从rails加载到远程mysql

我使用connection_ninja( https://github.com/cherring/connection_ninja )从我的rails应用程序连接到远程mysql数据库。 我的模型中有一个方法,它使用’load data local infile ..’从运行我的rails app的服务器加载一个csv文件到远程mysql db。 代码如下: class Product < ActiveRecord::Base @conn = use_connection_ninja(:rl_op) self.table_name = 'RlProduct' def self.update(file_path) sql = "LOAD DATA LOCAL INFILE '#{file_path}' INTO TABLE RlProduct FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\n' (name,price,productId)" @conn.connection().execute(sql) end end 这给了我以下错误: Mysql2::Error: The used command is not […]

如何从现有的Rails 4应用程序中删除ActiveRecord?

我开始使用Ruby on Rails项目,其中包括一个带有ActiveRecord和Neography的简单推荐系统。 现在我发现了neo4j.rb ,它将大大简化我的生活。 🙂 我现在才知道,我可以创建一个没有ActiveRecord的新应用程序,如下所示: rails new xyz -O 我可以这样做,并将我的大部分文件/代码粘贴到该新项目中。 或者有更简单的方法吗? 我还在考虑是否有必要采取这一步骤。 是否可以并行使用neo4j.rb和ActiveRecord? 我正在考虑使用ActiveRecord和我在neo4j中的推荐系统运行身份validation系统(例如Devise)。 有没有人有这种方法的经验?

如何在rails中指定和validation枚举?

我目前有一个模型Attend将有一个状态列,这个状态列只有几个值。 STATUS_OPTIONS = {:yes,:no,:maybe} 1)我不确定如何在用户插入Attend之前validation这一点? 基本上java中的枚举,但我怎么能在rails中这样做?

rails – 如何在保存后刷新关联

我有一个包含项目列表的类别。 项目有一个位置,类别有一个关系has_many:items,:order =>“position”。 当用户更新位置值时,我想看到它的位置。 我的位置是一个浮点数,允许在舍入数字之间移动。 pos=item.category.items.map(&:id) current_position=pos.index(id.to_i) item.save # want to refresh the relationship here pos_new=item.categoty.items.map(&:id) # grabbing this since just accessing item isn’t updated if positioning has changed item_new=Item.find(id) pos_new=item_new.category.items.map(&:id) new_position=pos_new.index(id) if current_position!=new_position is_moved=true # sent back in JSON to propagate a dynamic change. end 上述工作,但似乎真的很冗长。 有没有办法让我告诉项目保存类别关系需要刷新,因为订单可以更改?

为什么我在Rails 4中获得连接表的未知主键例外?

这些是我的模特: class Product has_many :line_items has_many :orders, :through => :line_items end class LineItem belongs_to :order belongs_to :product end class Order has_many :line_items has_many :products, :through => :line_items end 来自schema.rb: create_table “line_items”, id: false, force: true do |t| t.integer “order_id” t.integer “product_id” t.integer “quantity” t.datetime “created_at” t.datetime “updated_at” end 我刚刚升级到Rails 4,我的连接表停止工作。 如果我执行@order.line_items ,它会抛出exception“模型LineItem中的表line_items的未知主键”。 @order.products按预期工作。 我已经尝试删除并重新创建line_items表,我已经尝试安装protected_attributes gem,但没有任何改变。 […]

has_many:通过has_and_belongs_to_many关联

我试图在Ruby on Rails项目中执行以下操作: class FoodItem :food_categories end class FoodCategory < ActiveRecord::Base has_and_belongs_to_many :food_items belongs_to :place end class Place :food_category end 但调用实例方法some_food_item.places会给我以下错误: ActiveRecord::StatementInvalid: PGError: ERROR: column food_categories.food_item_id does not exist LINE 1: …laces”.id = “food_categories”.place_id WHERE ((“food_cate… : SELECT “places”.* FROM “places” INNER JOIN “food_categories” ON “places”.id = “food_categories”.place_id WHERE ((“food_categories”.food_item_id = 1)) 这很有道理 – 因为FoodItem和FoodCategory上的HABTM我有一个名为food_categories_food_items的映射表。 […]

在Rails 5.0.0.beta3,-api选项中使用accepts_nested_attributes_for时出现问题

我正在使用Rails 5.0.0.beta3,使用rails new上的-app选项构建一个仅限API的应用程序,而我在使用accepts_nested_attributes_for时遇到了问题。 在我的应用程序中,具有嵌套属性的对象的创建(或新的,然后保存!)失败,并显示父父对象必须存在的消息。 为了测试,我制作了一个新的应用程序,只使用了ANAF文档中的成员和post的测试用例: class Member < ApplicationRecord has_many :posts accepts_nested_attributes_for :posts end 和 class Post < ApplicationRecord belongs_to :member end (这些类定义是由Rails脚手架生成器生成的,所以inheritance自ApplicationRecord,而不是ActiveRecord :: Base,但是根据这篇文章 ,这并不重要。) 对于已定义的类别,以及创建和运行的匹配迁移,我启动了Rails控制台并按照文档中的步骤操作: params = { member: { name: ‘joe’, posts_attributes: [ { title: ‘Kari, the awesome Ruby documentation browser!’ }, { title: ‘The egalitarian assumption of the modern citizen’ }, […]

使ActiveRecord / Rails使用实际的mysql TIMESTAMP列

Rails’ :timestamp列类型; 它实际上只是一个别名:datetime 。 我正在使用mysql,我想使用实际的unix-timestamp TIMESTAMP列。 a)有没有一种很好的方法来设置它,除了使用SQL创建列? b)ActiveRecord是否会正确处理它(例如在必要时转换为Time ,接受unix时间戳Integer作为输入等)? 我应该期待得到什么,以及在哪里? 为什么: 速度。 这适用于聚合已使用unix时间戳的外部数据源的极其活跃的表。 转换为datetime(甚至首先转换为db字符串,通过2 gsub )会占用其大部分导入时间。 否则我可以做一个廉价的Integer#to_s调用。 时区。 我不想要他们。 我希望它存储时区 – 非常地存储; 处理时区是一种痛苦,除了在个人用户显示之前的最后阶段,与我的需求完全无关。 数据本身无需知道记录的时区。 尺寸。 这是一张大桌子。 TIMESTAMP的大小是DATETIME的一半。 是的,我仍然会在代码中进行updated_at计算,而不是mysql。 那部分不是瓶颈。 为什么你的’为什么不’是错的(先前说明,我不是要求出于无理由的原因:-P): “但TIMESTAMP自动更新”: 默认情况下这是唯一的,并且可以轻松关闭。 我实际上并没有使用Rails,只有ActiveRecord。 是的,这是基于实际的分析数据 ; 我不是在早期优化。 ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter#quote (在Quoting#quoted_date [如果通过Time ]或Mysql2Adapter#quote_string [如果预转换为to_s(:db) ])实际上是我的刮刀中耗费CPU最多的部分。 我想摆脱它。

activerecord通过关联找到

我试图从我的数据库中检索一个activerecord对象。 我的模特是 class User :account end 和 class Account < ActiveRecord::Base has_many :domains has_many :users end 和 class Domain < ActiveRecord::Base belongs_to :account end 现在我想根据用户名和域名检索用户(假设这些是User和Domain类的属性)。 也就是说 User.find(:first, :conditions =>{:username => “Paul”, :domains => { :name => “pauls-domain”}}) 我知道上面的代码不起作用,因为我必须提到有关域表的一些内容。 此外,用户和域之间的关联是一对多的(这可能使事情进一步复杂化)。 关于如何形成这个查询的任何想法?