从活动资源调用活动记录方法 (Ruby/Rails)

发布于 2024-11-24 02:35:56 字数 790 浏览 4 评论 0原文

对于非常广泛的标题问题表示歉意。

基本上,这是我之前关于 def 以及如何在实例化方法上调用它们的问题的延续。

基本上就是我现在的方式:

我在客户端设置了一个活动资源并使用 .save 发布它 然后,它通过服务器上的控制器并存储同一类的活动记录。

所以 MyResource-->save-->MyRecord

MyRecord 存储时包含一个包含简单字符串的状态列。 事情是 MyRecord 类有一个 def 名为

  def get_status
    puts status
  end #Amazing method, I know

在我看来,如果我想在 MyRecord 上执行 get_status,我所要做的就是这个。

(Please note this is client side)
@test = MyRecord.find(1)
@test.get_status

遗憾的是,情况并非如此,因为 @test 成为活动资源并且无法调用它没有的方法。 (注意:我的类实际上并不称为 MyRecord 和 MyResource,为了简单起见,它们只是标题,因为我宁愿理解解决方案,也不愿让别人为我解决它。)

是否有人愿意为我指出正确的方向来解释如何我从客户端调用活动记录方法。我是否完全错误地处理它?它应该在控制器而不是模型中处理吗?


旁注:是否有 .save 的替代方案?我的老板不喜欢它,原因我无法理解。 (注:他是领导,我是实习生,因此我不会争论或提出看似挑战的问题)

Apologies for the very broadly title question.

Basically this follows on from my earlier question about defs and how they are called on instantiated methods.

Basically the way I have it now:

I set up an active resource on client side and post it with .save
This then goes through my controller on server and stores an active record of the same class.

so MyResource-->save-->MyRecord

The MyRecord is stored with a status column containing a simple string.
Thing is MyRecord class has a def called

  def get_status
    puts status
  end #Amazing method, I know

In my mind If i wanted to execute the get_status on MyRecord, all I had to do was this.

(Please note this is client side)
@test = MyRecord.find(1)
@test.get_status

Sadly this is not the case as @test becomes an active resource and cant call a method it doesnt have.
(NOTE: My classes are not actually called MyRecord and MyResource, They are just title that for simplicity as I'd rather understand the solution than have someone solve it for me.)

Would anyone care to point me in the right direction to explain how I call the active record method from client side. Have I gone completely the wrong way about it and should it be processed in controller instead of model?


On a side note: Are there any alternatives to .save? My boss doesn't like it for reasons I cannot understand. (NOTE: he's a lead, I'm an intern therefore I don't argue or ask questions that seem like a challenge)

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

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

发布评论

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

评论(2

云朵有点甜 2024-12-01 02:35:57

老实说,ActiveResource 需要做一些工作...我必须在我工作的地方实现这个确切的功能,并最终使用基本的 http 在客户端和服务器之间共享代码来滚动我自己的半 RPC 库。

确实像 ruby-rpcDRb 可能是一个更好的选择。

Honestly, ActiveResource needs a little work... I had to implement this exact feature where I work and ended up rolling my own semi RPC lib using basic http to share code between client and server.

Really though something like ruby-rpc or DRb would probably be a better option.

桜花祭 2024-12-01 02:35:57

您可以将 ARecord 方法的结果包含在发送到 AResource 模型的数据中:

class MyRecord < ActiveRecord::Base
  ...
  def to_xml(options={})
    super(options.merge(:methods => [ :your_method_name_here ]))
  end
  ...
end

您需要在 AResource 模型中重新创建要在客户端上使用的任何其他方法。 (我不知道 puts 如何属于 ARecord 模型,但可以做任何能让你的船漂浮的事情。)

You can include the result of ARecord methods in the data shipped to your AResource model:

class MyRecord < ActiveRecord::Base
  ...
  def to_xml(options={})
    super(options.merge(:methods => [ :your_method_name_here ]))
  end
  ...
end

You'll need to re-create any other methods in the AResource model that you want to use on the client. (I don't know how a puts belongs in an ARecord model, but do whatever floats your boat.)

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