为什么 Ruby on Rails 在使用 has_many 和 Belongs_to 时附加一个空对象?
嘿,我正在写一个类似于 4chan 的网站。我有两个模型:Board
和 Picture
。一个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 Picture
s:
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 Picture
s 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 Picture
s, 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试图片.礼物?而不是.any?
Try pictures.present? instead of .any?
哈!
在我看来,添加
.all
就可以完成任务(并且order
也可以):Ha!
In my view, adding
.all
did the job (andorder
also works):