Heroku 上的 Rails HABTM 模型显示“model_id”、“name”和“时间戳”而不是简单地“名称”

发布于 2024-12-19 09:33:48 字数 548 浏览 2 评论 0原文

这一定很简单,但我就是找不到方法。

我有 2 个具有 HABTM 关系的模型。

Trip.rb

    has_and_belongs_to_many :categories 

Category.rb

    has_and_belongs_to_many :trips

Trip index.html.erb

    <%= trip.categories %> 

在我的本地计算机上一切正常 - 我只看到类别名称。

但是当我部署到heroku时,我看到的不是类别名称,而是

 [#<Category id: 1, name: "Surfing", created_at: "2011-10-20 12:28:57", updated_at: "2011-10-20 12:28:57">] 

有人知道如何解决这个问题吗? 多谢!

This must be quite simple, but I just can't find a way.

I have 2 models with HABTM relationship.

Trip.rb

    has_and_belongs_to_many :categories 

Category.rb

    has_and_belongs_to_many :trips

Trip index.html.erb

    <%= trip.categories %> 

everything is fine on my local machine - I see only the category name.

But when I deploy to heroku, instead of category name I see

 [#<Category id: 1, name: "Surfing", created_at: "2011-10-20 12:28:57", updated_at: "2011-10-20 12:28:57">] 

Anyone knows how to fix this ?
Thanks a lot!

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

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

发布评论

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

评论(1

挽你眉间 2024-12-26 09:33:49

我不确定为什么您会在本地看到 name,但您在 Heroku 上看到的是 to_strip 上隐式调用的结果。 categories 关联,它是一个类别记录数组。

# You could define the `to_s` of Category to return the name.
class Category
  def to_s
    name
  end
end

# or define a method to return a mapping of the category names:
class Trip
  # via an association extension
  has_and_belongs_to_many :categories do
    def names
      map(&:name)
    end
  end

  # or a simple instance method
  def category_names
    categories.map(&:name)
  end
end

Trip.first.categories.names #=> [cat1, cat2]
Trip.first.category_names   #=> [cat1, cat2]

但是您当前的模板仍然会将字符串Array放入输出中,例如:

["category1", "category2", "category3"]

您可能想要的更像是:

<%= trip.categories.map(&:name).to_sentence %>

这将导致:“category1、category2和category3”,或一些这样的。

I'm not sure why you would see the name locally, but what you're seeing on Heroku is the result of to_s being implicitly called on the trip.categories association, which is an array of category records.

# You could define the `to_s` of Category to return the name.
class Category
  def to_s
    name
  end
end

# or define a method to return a mapping of the category names:
class Trip
  # via an association extension
  has_and_belongs_to_many :categories do
    def names
      map(&:name)
    end
  end

  # or a simple instance method
  def category_names
    categories.map(&:name)
  end
end

Trip.first.categories.names #=> [cat1, cat2]
Trip.first.category_names   #=> [cat1, cat2]

But your current template is still going to drop an Array of strings into the output, like:

["category1", "category2", "category3"]

What you probably want is more like:

<%= trip.categories.map(&:name).to_sentence %>

Which would result in: "category1, category2, and category3", or some such.

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