为什么 Ruby on Rails 在使用 has_many 和 Belongs_to 时附加一个空对象?

发布于 2024-10-22 08:31:15 字数 1700 浏览 3 评论 0原文

嘿,我正在写一个类似于 4chan 的网站。我有两个模型:BoardPicture。一个Board有很多Picture

class Board < ActiveRecord::Base

  # Relationships
  has_many :pictures, :dependent => :destroy

end
class Picture < ActiveRecord::Base

  # Relationships
  belongs_to :board

end

我在我的视图中执行此操作boards/show.html.haml

- if @board.pictures.any?
  - @board.pictures.each do |picture|
    %article.picture-article{ :id => "picture-#{picture.id}" }
      - if picture.title?
        %header
          %h2= picture.title
      %img{ :src => picture.pic.url, :alt => (picture.title? ? picture.title : 'untitled pic') }/
      %pre~ picture.description
- else
  meh this board is still empty

在我漂亮的BoardsController中

def show
  @board = Board.find_by_short_name params[:id]
end

问题是总是显示一张空的图片。例如,我有一个 Board ,数据库中没有 Picture(使用 Picture.all 检查),但这是输出:

<article class='picture-article' id='picture-'>
  <img alt='untitled pic' src='/pics/original/missing.png'>
  <pre></pre>
</article>

但是,当我将其渲染为 JSON 时,输出没有 Picture,就像它应该的那样:

{
  "board": {
    "name": "n00bs",
    "created_at": "2011-03-16T17:14:32Z",
    "updated_at": "2011-03-16T17:14:32Z",
    "id": 14,
    "short_name": "0",
    "pictures": [
    ]
  }
}

我可以通过在执行 each< 之前简单地删除数组的最后一项来删除它。 /code> 但错误仍然存​​在,这是超级笨拙的。怎么会发生这种事呢?为什么@board.pictures总是附加一个空的Picture对象?

Hey, I am writing a website similar to 4chan. I have two models: Board and Picture. A Board has many Pictures:

class Board < ActiveRecord::Base

  # Relationships
  has_many :pictures, :dependent => :destroy

end
class Picture < ActiveRecord::Base

  # Relationships
  belongs_to :board

end

I do this in my view boards/show.html.haml:

- if @board.pictures.any?
  - @board.pictures.each do |picture|
    %article.picture-article{ :id => "picture-#{picture.id}" }
      - if picture.title?
        %header
          %h2= picture.title
      %img{ :src => picture.pic.url, :alt => (picture.title? ? picture.title : 'untitled pic') }/
      %pre~ picture.description
- else
  meh this board is still empty

And in my nice BoardsController:

def show
  @board = Board.find_by_short_name params[:id]
end

The problem is that always one Picture is shown which is empty. For example, I have a Board with no Pictures in the db (checked it with Picture.all), but this is the output:

<article class='picture-article' id='picture-'>
  <img alt='untitled pic' src='/pics/original/missing.png'>
  <pre></pre>
</article>

However, when I render it as JSON, the output is with no Pictures, like it should be:

{
  "board": {
    "name": "n00bs",
    "created_at": "2011-03-16T17:14:32Z",
    "updated_at": "2011-03-16T17:14:32Z",
    "id": 14,
    "short_name": "0",
    "pictures": [
    ]
  }
}

I can remove this by simply removing the last item of the array before doing an each but the bug still remains and this is super duper clumsy. How can this happen? Why does @board.pictures always append an empty Picture object?

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

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

发布评论

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

评论(2

百善笑为先 2024-10-29 08:31:15

尝试图片.礼物?而不是.any?

Try pictures.present? instead of .any?

蓝戈者 2024-10-29 08:31:15

哈!

在我看来,添加 .all 就可以完成任务(并且 order 也可以):

- @board.pictures.all.each do |picture|

Ha!

In my view, adding .all did the job (and order also works):

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