현재 액티브 레코드 협회에 질문이 있습니다.액티브 레코드 협회 일대 다
질문 (매우 stackoverflow와 유사)을 기반으로 주석 시스템을 만들고 있습니다.
나는 협회를보고 있는데 사용자는 많은 의견을 가지고 있으며 댓글은 사용자에게 속한다.
이제 실제로 댓글을 만들 때 user_id가 내 댓글의 user_id 필드에 포함되어야합니다. 맞습니까? 여기
내 스키마입니다 : 여기create_table "users", force: true do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.integer "question_id", limit: 255
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at"
t.datetime "updated_at"
t.string "provider"
t.string "uid"
t.string "name"
end
create_table "comments", force: true do |t|
t.integer "user_id" **THIS IS WHERE I AM EXPECTING A JOIN AND WHEN A USER MAKES A COMMENT TO HAVE THE USER_ID NUMBER STORED HERE
t.integer "question_id"
t.text "comment"
t.integer "upvotes"
t.datetime "created_at"
t.datetime "updated_at"
end
내 액티브 레코드 협회입니다
USER.RB :
class User < ActiveRecord::Base
has_many :questions
has_many :comments
has_many :votes
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable, :omniauthable,
:recoverable, :rememberable, :trackable, :validatable
def self.from_omniauth(auth)
where(auth.slice(:provider, :uid)).first_or_create do |user|
user.provider = auth.provider
user.uid = auth.uid
# user.username = auth.info.nickname
user.email = auth.info.email
user.name = auth.info.name
end
end
def self.new_with_session(params, session)
if session["devise.user_attributes"]
new(session["devise.user_attributes"], without_protection: true) do |user|
user.attributes = params
user.valid?
end
else
super
end
end
def password_required?
super && provider.blank?
end
def update_with_password(params, *options)
if encrypted_password.blank?
update_attributes(params, *options)
else
super
end
end
end
Comment.rb :
class Comment < ActiveRecord::Base
belongs_to :question
belongs_to :user
has_many :votes
end
내 최종 목표는 내 동료가하는 것입니다. 새 코멘트가 작성 될 때 user_id를 등록하고 사용자에게 주석을 연결하는 스키마.
는 지금, 내 USER_ID는 "전무"
**이
'user_id'가 'nil'인 컨텍스트 (예 : 코드 및 출력)를 공유하지 않았으므로이 질문에 대답하기가 어렵습니다. –
죄송합니다 피터,이게 당신이 찾고있는 것입니다, 이것은 레일즈 콘솔의 일반적인 쿼리입니다. Comment.last는 # –
HelloWorld
좋아, 그걸 어떻게 보는지 설명하지만 여전히 열어 둡니다. 어떻게 당신이 그것을 창조했는지에 관한 질문. 내 대답이 도움이 되었습니까? –