Postgresql 与Rails 3 - 数组中元素的 Update_attribute 会重新排序数组..为什么?
Postgresql / Rails 3 / Ruby 1.9.2 / OSX
class Deliverable < ActiveRecord::Base
has_many :models, :dependent => :destroy
def build_template
models << sect_letterhead << sect_ica << sect_project_description_exhibit
end
end
ruby-1.9.2-p180 :002 > d.models.first
=> #<Model id: 1, company_id: nil, deliverable_id: 1, model_id: nil, type: nil, kind: "cover_page", csv_file_name: nil, csv_content_type: nil, csv_file_size: nil, csv_updated_at: nil>
ruby-1.9.2-p180 :003 > d.models.first.update_attribute(:csv_file_name, "blah")
=> true
ruby-1.9.2-p180 :004 > d.models.first
=> #<Model id: 5, company_id: nil, deliverable_id: 1, model_id: nil, type: nil, kind: "opening_letter", csv_file_name: nil, csv_content_type: nil, csv_file_size: nil, csv_updated_at: nil>
每当我 update_attribute 模型时,该模型都会被推送到数组的末尾。 除了分配自定义排序属性之外,还有其他方法可以避免更改调用 update_attribute 时的数组顺序?我可以很容易地做到这一点,但我对数据库的行为感兴趣。
即使我从模型对象中删除时间戳,该行为仍然存在。
如果没有时间戳来跟踪刚刚更新的模型,这是如何发生的?或者当我调用 update_attribute 时,模型实际上是从数组中拉出并放回最后一个位置吗?
Postgresql / Rails 3 / Ruby 1.9.2 / OSX
class Deliverable < ActiveRecord::Base
has_many :models, :dependent => :destroy
def build_template
models << sect_letterhead << sect_ica << sect_project_description_exhibit
end
end
ruby-1.9.2-p180 :002 > d.models.first
=> #<Model id: 1, company_id: nil, deliverable_id: 1, model_id: nil, type: nil, kind: "cover_page", csv_file_name: nil, csv_content_type: nil, csv_file_size: nil, csv_updated_at: nil>
ruby-1.9.2-p180 :003 > d.models.first.update_attribute(:csv_file_name, "blah")
=> true
ruby-1.9.2-p180 :004 > d.models.first
=> #<Model id: 5, company_id: nil, deliverable_id: 1, model_id: nil, type: nil, kind: "opening_letter", csv_file_name: nil, csv_content_type: nil, csv_file_size: nil, csv_updated_at: nil>
Whenever I update_attribute a model, that model is pushed to the end of the array. Is there any way other than assigning a custom sort attribute to avoid changing the array order when update_attribute is called? I could fairly easily do this, but I am interested in the behavior of the database.
Even when I remove the timestamps from the model object, the behavior remains.
How does this occur if there are no timestamps to keep track of which model was just updated? Or is the model actually pulled out of the array and put back in the last position when I call update_attribute?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不是 PostgreSQL 专家,但我注意到了同样的行为。我的理解是,PostgreSQL 本身没有默认顺序(而 MySQL 是可靠的——据我所知——按主键排序)。 PostgreSQL 的数据格式可能意味着修改的记录会移到末尾。
简而言之:这不是 Rails 的事情 - 这是 PostgreSQL 的事情,因此时间戳并不重要。
无论哪种方式,它都会迫使您明确想要退回的物品的顺序。这不是一个糟糕的主意。
I'm no PostgreSQL expert, but I've noticed the same behaviour. My understanding is that PostgreSQL doesn't have a default order, per se (whereas MySQL is reliable - as far as I know - in ordering by the primary key). PostgreSQL's data format maybe means that modified records are shifted to the end.
In short: this isn't a Rails thing - it's a PostgreSQL thing, hence why timestamps don't matter.
Either way, it forces you to be clear about the order you want items returned in. Not a terrible idea.