ユーザーに所属させたい投稿モデルがあります。私がそれをしたとき、ビューでユーザーのものを参照できませんでした。私はDeviseを使っています。
投稿モデル:
class Post < ActiveRecord::Base
attr_accessible :album, :artist, :song
belongs_to :user
validates_presence_of :album, :artist, :song
end
ユーザーモデル:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable
# Setup accessible (or protected) attributes for your model
attr_accessible :username, :password, :password_confirmation, :remember_me
has_many :posts
validates_uniqueness_of :username
validates_presence_of :username, :password, :password_confirmation
end
そして、ユーザー名を で参照しようとしました<%= post.user.username %>
。私も試し<%= post.user%>
てみ<%= post.user.name %>
ました。
更新user_id
:テーブルに追加しました。しかし、投稿すると、username
とのuser_id
値が設定されません。