Ruby on Rails - 覆盖关联 ID 创建过程

发布于 2024-09-10 12:57:02 字数 1085 浏览 4 评论 0原文

我试图覆盖 Rails 应用和 id 到关联对象的方式,例如:

有 2 个简单的模型:

class Album < ActiveRecord::Base
  has_many :photos
end

class Photo < ActiveRecord::Base
  belongs_to :album
end

然后我想这样做:

album = Album.new :title => 'First Album'
album.photos.build
album.save #=> true

在这种情况下,我创建了一个插件来覆盖 id 属性并替换它转换为散列字符串,所以我想要做的是找到将这个 album_id 替换为我的自定义方法而不是 int 的方法,并且能够在保存之前进行转换。 但我想在 Rails 结构内进行全局操作,因为它是一种插件,我想让此操作在动态模型上工作,这就是为什么我无法在模型上创建 before_save 验证。

我不确定这是否容易理解,但我希望有人可以帮助我。

这是我当前表格的屏幕截图,以便您可以看到发生了什么:

SQLite3 DB http://cl.ly/1j3U/content

所以你可以看到 album_id 它是保存时被替换为我的自定义 ruby​​ 对象...我禁用了该插件,然后它正常保存为记录 11 和 12...

我只想执行 Rails 操作并使用我的自定义方法进行转换,

def rails_association_replaced_method(record)
   #take the record associations and apply a to_i custom method before save
   super(record)
end

例如像这样:)

好吧,我希望这不会变得太复杂

干杯

I'm trying to override the way rails apply and id to an associated object, for example:

There are 2 simple models:

class Album < ActiveRecord::Base
  has_many :photos
end

class Photo < ActiveRecord::Base
  belongs_to :album
end

And then I want to do this:

album = Album.new :title => 'First Album'
album.photos.build
album.save #=> true

On this case I've created a plugin that overrides the id property and replaces it to a hashed string, so what I want to do is find the methods where this album_id is being replaced for my custom method instead of the int and be able to converted before it's saved.
But I want to act globally inside Rails structure because since it will be a sort of plugin I want to make this action work on dynamic models, that's why I can't create an before_save validation on the model.

I'm not sure if it's easy to understand, but I hope someone could help me on that..

Here's a screenshot of my current table so you can see what is happening:

SQLite3 DB http://cl.ly/1j3U/content

So as you can see the album_id it's being replaced for my custom ruby object when its saved...I've disabled the plugin and then it saved normally with records 11 and 12...

I want just act on a rails action and converted with my custom methods, something like

def rails_association_replaced_method(record)
   #take the record associations and apply a to_i custom method before save
   super(record)
end

something like this :)

Well I hope this didn't get too complicated

Cheers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

俯瞰星空 2024-09-17 12:57:02

看来,如果我只覆盖 ActiveRecord::Base save 方法,如果处理正确,就可以完成这项工作

define_method 'save' do
  int_fields = self.class.columns.find_all { |column| column.type == :integer }
  int_fields.each do |field|
    if self.attributes[field.name]
      self.attributes[field.name] = self.attributes[field.name].to_i
    end
  end
  super
end

,这将替换当前模型中的所有整数字段,对结果应用 to_i 方法。

It seems if I only override theActiveRecord::Base save method do the job if handled properly

define_method 'save' do
  int_fields = self.class.columns.find_all { |column| column.type == :integer }
  int_fields.each do |field|
    if self.attributes[field.name]
      self.attributes[field.name] = self.attributes[field.name].to_i
    end
  end
  super
end

And this shall replace all the integer fields from the Current Model applying a to_i method over the result.

泪痕残 2024-09-17 12:57:02

Rails 对这种默认设置的更改不友好。你在这里的最终目标是什么?

Rails is unfriendly to that kind of change to the defaults. What's your end goal here?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文