0
我在rails的模型中遇到了關聯問題。模型看不到has_many關係,得到
我有這樣一段代碼
class Member < ApplicationRecord
has_many :rooms
has_many :tokens, dependent: :destroy
has_secure_password
//[...] - some validations not according to my model
然後在我的控制器我有
def create
unique_id = SecureRandom.uuid
@room = @current_member.rooms.new(unique_id: unique_id)
@room_details = RoomDetail.new(video_url: 'test', room: @room)
if @room.save
render json: @room, status: :created, location: @room
else
render json: @room.errors, status: :unprocessable_entity
end
end
最近一切工作,因爲它應該。現在,創建令牌表+添加後,以模型,它說
"status": 500,
"error": "Internal Server Error",
"exception": "#<NoMethodError: undefined method `rooms' for #<Member::ActiveRecord_Relation:0x00560114fbf1d8>>",
我得到的用戶使用這種方法。
def authenticate_token
authenticate_with_http_token do |token, options|
@current_member = Member.joins(:tokens).where(:tokens => { :token => token })
end
end
太棒了,它的作品像魅力!我一直在用''''''嘗試一些東西,但絕對不會在那裏:D謝謝!編輯:我使用RubyMine,因爲我習慣了Java中的IntelliJ IDEA –