Rails 中不同“作者”的多态关联型号

发布于 2024-11-26 17:30:25 字数 1049 浏览 1 评论 0原文

多态关联(此处:评论)本身如何与不同类型的作者相关联?

从……开始

class Comment < ActiveRecord::Base
  belongs_to :commentable, :polymorphic => :true
end

,我需要有一位作者来自模型Hosts,ID 5,另一位作者来自Users,2

路径助手会是什么样子

<%= link_to comment.author.name, user_path(comment.author) %>

当“user_path”或“host_path”是动态的(取决于作者模型)时,

编辑***

有事件、地点等可以有这样的评论:

  has_many :comments, :as => :commentable

对于多态评论模型,我想添加 ID 和类型来引用评论的作者:

  create_table "comments", :force => true do |t|
    t.text     "content"
    t.integer  "commentable_id"
    t.string   "commentable_type"
    t.integer  "author_id"
    t.string   "author_type"
  end

事件页面显示评论,然后单击作者姓名应该将我带到 User(5) 或 AnotherModel(2),具体取决于评论的作者。

我想知道大家遇到这种情况都是怎么处理的。我是否应该考虑添加第二个多态“中间层”,例如“配置文件”,它可以容纳子类“用户”、“主机”等?

编辑2

仅拥有一个用户模型显然会让这里的生活变得更轻松,但由于其他原因而无法做到这一点。总的来说,我感兴趣的是如何很好地组织这一切。

How would a Polymorphic Association (here: Comments) be itself associated with different types of Authors?

Starting from …

class Comment < ActiveRecord::Base
  belongs_to :commentable, :polymorphic => :true
end

… I'd need to have one author being from model Hosts, ID 5 and another one from Users, 2.

How could path helpers look like…

<%= link_to comment.author.name, user_path(comment.author) %>

… when "user_path" or "host_path" are dynamic, depending on the author model?

EDIT***

There are Events, Places etc. that can have comments like so:

  has_many :comments, :as => :commentable

To the polymorphic Comment model i would like to add IDs and Types to refer to Authors of comments:

  create_table "comments", :force => true do |t|
    t.text     "content"
    t.integer  "commentable_id"
    t.string   "commentable_type"
    t.integer  "author_id"
    t.string   "author_type"
  end

An Events page displays comments, and clicking on an author name should take me either to User(5) oder AnotherModel(2), depending on who wrote the comment.

I'd like to know how everybody handles this kind of situation. Should I think about adding a second polymorphic "middle layer", such as "profile", that could hold the subclasses "User", "Host" and so forth?

EDIT 2

Having only one User model would make life easier here obviously, but that cannot be done for other reasons. And in general i'm interested how this could be organized well.

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

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

发布评论

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

评论(3

笑饮青盏花 2024-12-03 17:30:25

简单地说

<%= link_to comment.author.name, comment.author %>

应该可以解决问题。如果您想要更大的灵活性,我建议

link_to ..., action_in_host_path(comment.author) if comment.author.kind_of? Host

Simply putting

<%= link_to comment.author.name, comment.author %>

should do the trick. If you want more flexibility, I would suggest

link_to ..., action_in_host_path(comment.author) if comment.author.kind_of? Host
梦幻的心爱 2024-12-03 17:30:25

这是我过去使用过的(在视图助手中),但它使用了 eval:

def model_path(model, options = {})
    {:format => nil}.merge(options)
    format = (options[:format].nil? ? 'nil' : "'#{options[:format].to_s}'")
    eval("#{model.class.to_s.downcase}_path(#{model.id}, :format => #{format})")
end

像这样使用:

<%= link_to comment.author.name, model_path(comment.author) %>

This is what I've used in the past (in a view helper), but it uses an eval:

def model_path(model, options = {})
    {:format => nil}.merge(options)
    format = (options[:format].nil? ? 'nil' : "'#{options[:format].to_s}'")
    eval("#{model.class.to_s.downcase}_path(#{model.id}, :format => #{format})")
end

Use like this:

<%= link_to comment.author.name, model_path(comment.author) %>
謌踐踏愛綪 2024-12-03 17:30:25

polymorphic_url 有帮助吗?

<%= link_to comment.author.name, polymorphic_url(comment.commentable)
%>

Would polymorphic_url help?

<%= link_to comment.author.name, polymorphic_url(comment.commentable)
%>

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