从 Rails 中的关系数据库中提取信息

发布于 2024-09-02 13:29:57 字数 954 浏览 6 评论 0原文

我正在尝试从专辑数据库中提取艺术家的姓名。 这是我的两个模型

class Album < ActiveRecord::Base

  belongs_to :artist

  validates_presence_of :title
  validates_length_of :title, :minimum => 5
 end

class Artist < ActiveRecord::Base

  has_many :albums

end

,这是专辑控制器

 def index
 @ albums = Album.all

 respond_to do |format|
   format.html # index.html.erb
   format.xml  { render :xml => @albums }
 end
end

和索引视图:

<% @albums.each do |album| %>
  <tr>
    <td><%=h album.id %></td>
    <td><%=h album.title %></td>
    <td><%=h album.artist.name %></td>
  </tr
<% end %>

我的最终结果 html 对于艺术家领域来说是这样的!

#<Artist:0x000001022e4868>   

如果我将其设置为artist.name 我明白了:

undefined method `name' for nil:NilClass

我做错了什么?

I am trying to pull the name of the Artist from the Albums database.
These are my two models

class Album < ActiveRecord::Base

  belongs_to :artist

  validates_presence_of :title
  validates_length_of :title, :minimum => 5
 end

class Artist < ActiveRecord::Base

  has_many :albums

end

And here is the Albums Controller

 def index
 @ albums = Album.all

 respond_to do |format|
   format.html # index.html.erb
   format.xml  { render :xml => @albums }
 end
end

And the View from the index:

<% @albums.each do |album| %>
  <tr>
    <td><%=h album.id %></td>
    <td><%=h album.title %></td>
    <td><%=h album.artist.name %></td>
  </tr
<% end %>

My end result html is coming out like this for the artist field!

#<Artist:0x000001022e4868>   

and if I set it to artist.name
I get this:

undefined method `name' for nil:NilClass

What am I doing wrong?

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

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

发布评论

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

评论(5

浮光之海 2024-09-09 13:29:58

您的艺术家与专辑的关系正在发生一些变化。如果您收到该错误,您将得到一个零艺术家,这意味着密钥没有保存在表中。手动检查你的表并确保关系存在,如果不存在那么你就找错了地方,你应该修复你的保存。

Something is going on the saving of your artists->album relationship. If you're getting that error you're getting a nil artist back which means the key isn't being saved in the table. Check your table manually and make sure the relation is there if it's not then you're looking in the wrong place, you should be fixing your save.

无风消散 2024-09-09 13:29:57

您需要做类似的事情:

<%=h album.artist.name %>

可以这么说,您使用它的方式正在显示整个对象。

You need to do something like:

<%=h album.artist.name %>

The way you used it you are displaying whole object, so to speak.

寻找我们的幸福 2024-09-09 13:29:57

听起来好像您有一个没有艺术家的专辑(artist_id 为空或设置为不再存在的artist_id)。

您可以尝试:

<%= h album.artist ? album.artist.name : 'N/A' %>

Sounds like you have an Album without an Artist (either artist_id is null or set to an artist_id that no longer exist).

You can try:

<%= h album.artist ? album.artist.name : 'N/A' %>
森罗 2024-09-09 13:29:57

前面列举的内容的另一种编写方式。

<%= h album.artist.name unless album.artist.blank? %>

我建议进入脚本/控制台并手动逐步完成提取所有文章的过程,然后打印出所有艺术家姓名。

顺便说一句,如果您在生产中运行此代码,您可能应该使用预加载,

 @ albums = Album.find(:all, :includes => [:artist]) 

这会更有效。

Another way to write what was enumerated earlier.

<%= h album.artist.name unless album.artist.blank? %>

I would recommend going into script/console and manually stepping through the process of pulling all your articles and then printing out all the artist names.

BTW, if you're running this code in production you should probably use eager loading

 @ albums = Album.find(:all, :includes => [:artist]) 

This will be much more efficient.

浅浅 2024-09-09 13:29:57

您的数据库表中是否确实正确设置了数据?如果您的艺术家和专辑模型保存正确,那么它应该看起来像这样:

artists table

id|name
---------------------
 1|The Beatles
 2|The Rolling Stones

albums table

id|artist_id|title
--------------------------------------------------
 1|1        |The White Album
 2|2        |Exile on Main Street
 3|1        |Sgt. Pepper's Lonely Hearts Club Band

Do you actually have data correctly set up in your database tables? If your Artist and Album models are being saved correctly then it should look something like this:

artists table

id|name
---------------------
 1|The Beatles
 2|The Rolling Stones

albums table

id|artist_id|title
--------------------------------------------------
 1|1        |The White Album
 2|2        |Exile on Main Street
 3|1        |Sgt. Pepper's Lonely Hearts Club Band
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文