Tag: 显示

从用户标识中检索用户名

嗨,我有一个post模型,其中post属于用户,用户has_manypost。 posts表有一个user_id 在我的节目帖中,我有: 我得到用户ID谁发布这个工作正常。 当Users表包含列User_name时,如何获取用户名?我是否需要将post_id添加到Users或? class User < ActiveRecord::Base devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable attr_accessible :email, :password, :username, :password_confirmation, :remember_me has_one :profile has_many :orders has_many :posts end class Post < ActiveRecord::Base belongs_to :user attr_accessible :content, :title, :user_id validates :title, presence: true, length: { minimum: 5 } end 在我的post控制器我有 def create @post = Post.new(params[:post]) […]