标签: activerecord

在ActiveRecord中覆盖create上的id

是否有任何方法可以在创建时覆盖模型的id值?就像是:

Post.create(:id => 10, :title => 'Test')
Run Code Online (Sandbox Code Playgroud)

会是理想的,但显然不会奏效.

activerecord ruby-on-rails

104
推荐指数
8
解决办法
5万
查看次数

为什么所有的Active Record都讨厌?

随着我越来越多地了解OOP,并开始实施各种设计模式,我不断回到人们讨厌Active Record的案例.

通常,人们说它不能很好地扩展(引用Twitter作为他们的主要例子) - 但没有人真正解释为什么它不能很好地扩展; 和/或如何在没有缺点的情况下实现AR的优点(通过类似但不同的模式?)

希望这不会变成关于设计模式的神圣战争 - 我想知道的只是****特别****Active Record有什么问题.

如果它不能很好地扩展,为什么不呢?

还有什么其他问题?

oop activerecord design-patterns ruby-on-rails

103
推荐指数
5
解决办法
2万
查看次数

Rails - 验证关联的存在?

我有一个模型A与另一个模型B有一个"has_many"关联.我有一个业务要求,插入A需要至少1个相关记录到B.是否有一个方法我可以调用以确保这是真的,或者我是否需要编写自定义验证?

validation activerecord ruby-on-rails ruby-on-rails-3

102
推荐指数
4
解决办法
6万
查看次数

如何将ActiveRecord结果转换为哈希数组

我有一个查找操作的ActiveRecord结果:

tasks_records = TaskStoreStatus.find(
  :all,
  :select => "task_id, store_name, store_region",
  :conditions => ["task_status = ? and store_id = ?", "f", store_id]
)
Run Code Online (Sandbox Code Playgroud)

现在我想将此结果转换为像这样的哈希数组:

[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" }
Run Code Online (Sandbox Code Playgroud)

这样我就可以遍历数组并向哈希添加更多元素,然后将结果转换JSON为我的API响应.我怎样才能做到这一点?

arrays hash activerecord

102
推荐指数
3
解决办法
9万
查看次数

如何在Rails控制台中显示SQL查询?

当我在控制台中运行查询(例如MyModel.where(...)record.associated_things)时,如何查看正在运行的实际数据库查询,以便我可以更好地了解正在发生的事情?

activerecord ruby-on-rails

98
推荐指数
6
解决办法
5万
查看次数

渴望加载多态

使用Rails 3.2,这段代码出了什么问题?

@reviews = @user.reviews.includes(:user, :reviewable)
.where('reviewable_type = ? AND reviewable.shop_type = ?', 'Shop', 'cafe')
Run Code Online (Sandbox Code Playgroud)

它引发了这个错误:

无法急切加载多态关联:可审核

如果我删除reviewable.shop_type = ?条件,它的工作原理.

如何基于reviewable_typereviewable.shop_type(实际上shop.shop_type)进行过滤?

activerecord ruby-on-rails ruby-on-rails-3

98
推荐指数
3
解决办法
4万
查看次数

在模型中使用帮助器:如何包含辅助依赖项?

我正在编写一个处理来自文本区域的用户输入的模型.根据http://blog.caboo.se/articles/2008/8/25/sanitize-your-users-html-input的建议,我在使用before_validate保存到数据库之前清理模型中的输入打回来.

我模型的相关部分如下所示:

include ActionView::Helpers::SanitizeHelper

class Post < ActiveRecord::Base {
  before_validation :clean_input

  ...

  protected

  def clean_input
    self.input = sanitize(self.input, :tags => %w(b i u))
  end
end
Run Code Online (Sandbox Code Playgroud)

不用说,这不起作用.当我尝试保存新帖子时出现以下错误.

undefined method `white_list_sanitizer' for #<Class:0xdeadbeef>
Run Code Online (Sandbox Code Playgroud)

显然,SanitizeHelper创建了一个HTML :: WhiteListSanitizer的实例,但是当我将它混合到我的模型中时,它找不到HTML :: WhiteListSanitizer.为什么?我该怎么做才能解决这个问题?

ruby activerecord model ruby-on-rails

96
推荐指数
6
解决办法
9万
查看次数

如何在Rails中获取属性的原始值

有没有办法获得ActiveRecord属性的原始值(=从数据库加载的值)?

在观察者中我想要这样的东西

before_save object
  do_something_with object.original_name
end
Run Code Online (Sandbox Code Playgroud)

任务是在更新时从哈希表中删除对象(事实上,将其移动到表中的另一个键).

activerecord ruby-on-rails

95
推荐指数
4
解决办法
3万
查看次数

在raails 4中使用has_many:through:uniq时的弃用警告

使用时,Rails 4引入了弃用警告:uniq => true with has_many:through.例如:

has_many :donors, :through => :donations, :uniq => true
Run Code Online (Sandbox Code Playgroud)

产生以下警告:

DEPRECATION WARNING: The following options in your Goal.has_many :donors declaration are deprecated: :uniq. Please use a scope block instead. For example, the following:

    has_many :spam_comments, conditions: { spam: true }, class_name: 'Comment'

should be rewritten as the following:

    has_many :spam_comments, -> { where spam: true }, class_name: 'Comment'
Run Code Online (Sandbox Code Playgroud)

重写上述has_many声明的正确方法是什么?

activerecord ruby-on-rails ruby-on-rails-4 rails-activerecord

95
推荐指数
2
解决办法
2万
查看次数

如何实现has_many:通过与Mongoid和mongodb的关系?

使用Rails指南中的这个修改示例,如何使用mongoid建模关系"has_many:through"关联?

挑战是mongoid不支持has_many:通过ActiveRecord.

# doctor checking out patient
class Physician < ActiveRecord::Base
  has_many :appointments
  has_many :patients, :through => :appointments
  has_many :meeting_notes, :through => :appointments
end

# notes taken during the appointment
class MeetingNote < ActiveRecord::Base
  has_many :appointments
  has_many :patients, :through => :appointments
  has_many :physicians, :through => :appointments
end

# the patient
class Patient < ActiveRecord::Base
  has_many :appointments
  has_many :physicians, :through => :appointments
  has_many :meeting_notes, :through => :appointments
end

# the appointment
class Appointment < ActiveRecord::Base
  belongs_to :physician
  belongs_to :patient …
Run Code Online (Sandbox Code Playgroud)

activerecord ruby-on-rails data-modeling mongodb mongoid

94
推荐指数
3
解决办法
2万
查看次数