Ruby on rails Rails:通过关联使用has\u many:显示对象名称
我有三个模型(如下所列):用户、线程和一个称为发布的连接 我正在成功列出用户的所有出版物,并显示出版物的用户id和线程id 我不想列出发布的信息,而是想使用每个发布的线程id显示每个发布的线程名称 以下是我的用户模型:Ruby on rails Rails:通过关联使用has\u many:显示对象名称,ruby-on-rails,ruby,ruby-on-rails-3,Ruby On Rails,Ruby,Ruby On Rails 3,我有三个模型(如下所列):用户、线程和一个称为发布的连接 我正在成功列出用户的所有出版物,并显示出版物的用户id和线程id 我不想列出发布的信息,而是想使用每个发布的线程id显示每个发布的线程名称 以下是我的用户模型: class User < ActiveRecord::Base devise :database_authenticatable, :registerable, :confirmable, :recoverable, :rememberabl
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
下面是我的索引视图中的代码,其中列出了用户的所有出版物:
%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
请使用关联关系表
%table
%tr
%th Publication
%th User
%th Created at
- @publication.each do |publication|
%tr
%td= publication.thread.name
%td= publication.user.username
%td= publication.created_at
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
%table
%tr
%th Publication
%th User
%th Created at
- @publication.each do |publication|
%tr
%td= publication.thread.name
%td= publication.user.username
%td= publication.created_at