活动记录关联错误 - 未定义方法

时间:2014-01-05 20:50:04

标签: ruby-on-rails associations

我有以下代码行似乎没问题。

<% current_user.blockedshows.map(&:tvshows).each_with_index do |blocked, index| %>

但是,当我在blocked.titleimage_tag(blocked.image)上调用时,(下面的完整代码)我收到以下错误:undefined method title for <ActiveRecord::Associations::CollectionProxy::ActiveRecord_Associations_CollectionProxy_Tvshow:0x007fd4e24b9448>

查看

<%= blocked.title %>
<%= image_tag(blocked.image) %> </br>

 Tweets containing the following keywords will be removed from your timeline: </br>

    <%@keywords = blocked.phrases.map(&:text) %>
    <%=@keywords %> </br>

Blockedshow模型

class Blockedshow < ActiveRecord::Base
  has_many :tvshows
  belongs_to :user
end

Tvshow模型

class Tvshow < ActiveRecord::Base
    has_many :phrases
    belongs_to :blockedshow
end

Tvshow表

class CreateTvShows < ActiveRecord::Migration
  def change
    create_table :tvshows do |t|
      t.string :title
      t.string :image 

      t.timestamps
    end
  end
end

1 个答案:

答案 0 :(得分:1)

那是因为tvshows本身是一个ActiveRecord关联对象。 你还需要遍历它:

current_user.blockedshows.map(&:tvshows).each_with_index do |blocked, index|
  blocked.each do |b|
    <%= b.title %>
  end 
end