0
我有三個模型(如下):用戶,線程和聯接稱爲出版物。的Rails:Display對象名稱中使用的has_many:通過關聯
我成功列出了所有用戶的出版物,並且顯示了出版物的user_id和thread_id。
而不是列出發佈的信息,我想顯示每個發佈的線程名稱,使用每個發佈的thread_id。
下面是我的用戶模型:
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable, :confirmable,
:recoverable, :rememberable, :trackable, :validatable
attr_accessible :email, :password, :password_confirmation,
:remember_me, :username, :profile_image,
:first_name, :last_name
has_many :publications
has_many :threads, :through => :publications
end
下面是我的出版物型號:
class Publication < ActiveRecord::Base
attr_accessible :thread_id, :user_id
belongs_to :thread
belongs_to :user
end
下面是我的線程模型:
class Thread < ActiveRecord::Base
attr_accessible :description, :name, :tag_list, :thumbnail_image, :status, :thread_type, :subtitle, :summary
has_one :publication
has_one :user, :through => :publication
end
下面是我的#INDEX行動,在我的threads_controller.rb中:
def index
@user = current_user
@threads = @user.threads.all
@publications = @user.publications.all
end
下面是我的索引視圖,在這裏我列出了所有用戶的出版物代碼:
%table
%tr
%th Publication
%th User
%th Created at
- @publication.each do |publication|
%tr
%td= publication.thread_id
%td= publication.user_id
%td= publication.created_at