Tag: activerecord

MongoDB与MySQL

我曾经使用MySQL构buildRuby on Rails应用程序。 MongoDB目前变得越来越有名,现在我开始尝试。 问题是,我不知道MongoDB是如何工作的基础理论(如果它使用mongoidgem, 所以我想比较使用MySQL + ActiveRecord和mongoid gem生成的模型之间的性能,谁能帮我弄明白?

Rails的ActiveRecord:连接与左连接,而不是INNER JOIN

我有这个代码 User.find(:all, :limit => 10, :joins => :user_points, :select => "users.*, count(user_points.id)", :group => "user_points.user_id") 其中生成以下SQL SELECT users.*, count(user_points.id) FROM `users` INNER JOIN `user_points` ON user_points.user_id = users.id GROUP BY user_points.user_id LIMIT 10 是否有可能使用LEFT JOIN而不是INNER JOIN,除了User.find_by_sql和manualy键入查询?

Rails创build或更新魔法?

我有一个名为CachedObject的类,它存储了通过键索引的generics序列化对象。 我想要这个类来实现一个create_or_update方法。 如果find一个对象,它将更新它,否则它将创build一个新的。 有没有办法在Rails中做到这一点,或者我必须写我自己的方法?

“警告:不能批量分配受保护的属性”

我使用RESTful技术来生成一个模型(实际上,我正在使用Devise gem,这对我来说是这样做的),并且我在模型中添加了名为first_name和last_name的新字段。 移民进展良好。 我在模型中添加了attr_accessor:first_name,:last_name,并预计它会工作。 但是当我尝试用Doctor.create({:first_name =>“MyName”})等等批量分配新的实例时,我收到错误,说我不能批量分配受保护的属性。 我认为使用attr_accessor的重点是解决模型字段的保护问题。 你能帮我理解这个信息吗? 编辑:哦,顺便说一句,logging也不会创build。 我以为他们应该是因为这只是一个警告,但他们不在数据库上。 编辑2:这是我的模型 class Doctor < User has_many :patients has_many :prescriptions, :through=> :patients validates_presence_of :invitations, :on => :create, :message => "can't be blank" attr_accessor :invitations end 和架构,它没有first_name和last_name,因为它们是在医生的祖先users表中创build的。 我用单表inheritance。 create_table :doctors do |t| t.integer :invitations t.timestamps end 这是迁移到更改用户表 add_column :users, :first_name, :string add_column :users, :last_name, :string add_column :users, […]

删除基于多列的重复logging?

我使用Heroku托pipe我的Ruby on Rails应用程序,出于某种原因,我可能有一些重复的行。 有没有办法删除重复的logging基于2个或更多的标准,但只保留1重复的集合logging? 在我的使用案例中,我在数据库中有一个汽车制造和模型关系。 Make Model — — Name Name Year Trim MakeId 我想要删除所有具有相同名称,年份和修剪的模型logging,但保留其中的一个logging(意思是,我需要logging,但只有一次)。 我正在使用Heroku控制台,所以我可以轻松地运行一些活动的logging查询。 有什么build议么?

如何将activerecord结果转换为散列数组

作为查找操作的结果,我有一个活动的logging结果 tasks_records = TaskStoreStatus.find(:all,:select => "task_id, store_name, store_region", :conditions => ["task_status = ? and store_id= ?","f" ,store_id]) 现在我想将这个结果转换成如下所示的散列数组 [0] -> { :task_d => 10, :store_name=> "Koramanagala", :store_region=> "India" } [1] -> { :task_d => 10, :store_name=> "Koramanagala", :store_region=> "India" } [2] -> { :task_d => 10, :store_name=> "Koramanagala", :store_region=> "India" } 因此,我将能够遍历数组并向散列中添加更多元素,然后将结果转换为JSON以用于我的API响应!

如何在rails中使用dynamic绑定来执行原始更新sql

我想要执行一个更新原始的SQL,如下所示: update table set f1=? where f2=? and f3=? 这个SQL将由ActiveRecord::Base.connection.execute执行,但我不知道如何将dynamic参数值传递给方法。 有人能给我任何帮助吗?

正则expression式在RoR 4中进行了validation

有以下代码: class Product < ActiveRecord::Base validates :title, :description, :image_url, presence: true validates :price, numericality: {greater_than_or_equal_to: 0.01} validates :title, uniqueness: true validates :image_url, allow_blank: true, format: { with: %r{\.(gif|jpg|png)$}i, message: 'URL must point to GIT/JPG/PNG pictures' } end 它的工作,但是当我尝试使用“耙testing”来testing它时,我会看到这个消息: rake aborted! The provided regular expression is using multiline anchors (^ or $), which may present a […]

Rails 3跳过validation和callback

我有一个特别复杂的模型,包含validation和callback定义。 业务需求现在需要添加新logging的特定场景需要跳过validation和callback。 什么是最好的方法来做到这一点?

如何将logging添加到has_many:通过rails中的关联

class Agents << ActiveRecord::Base belongs_to :customer belongs_to :house end class Customer << ActiveRecord::Base has_many :agents has_many :houses, through: :agents end class House << ActiveRecord::Base has_many :agents has_many :customers, through: :agents end 如何添加到Customer的业务Agents模型? 这是最好的方法吗? Customer.find(1).agents.create(customer_id: 1, house_id: 1) 上面的工作从控制台罚款,但是,我不知道如何在实际应用中实现这一点。 设想一个表格填充为客户,也以house_id作为input。 那么我在我的控制器中执行以下操作? def create @customer = Customer.new(params[:customer]) @customer.agents.create(customer_id: @customer.id, house_id: params[:house_id]) @customer.save end 总的来说,我很困惑如何在has_many :through添加logginghas_many :through表?