質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

RubyGems

RubyGemsはRubyによるプログラミングのためのパッケージマネジメントツールです。ユーザはこれを使用することで、Rubyライブラリのダウンロードやアップデートや、依存関係の自動解決が可能になります。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby on Rails 4

Ruby on Rails4はRubyによって書かれたオープンソースのウェブフレームワークです。 Ruby on Railsは「設定より規約」の原則に従っており、効率的に作業を行うために再開発を行う必要をなくしてくれます。

Q&A

解決済

1回答

4873閲覧

Rails : ActiveAdminでのuninitialized constantエラー (modelの関連付けに問題?)

innjera

総合スコア132

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

RubyGems

RubyGemsはRubyによるプログラミングのためのパッケージマネジメントツールです。ユーザはこれを使用することで、Rubyライブラリのダウンロードやアップデートや、依存関係の自動解決が可能になります。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby on Rails 4

Ruby on Rails4はRubyによって書かれたオープンソースのウェブフレームワークです。 Ruby on Railsは「設定より規約」の原則に従っており、効率的に作業を行うために再開発を行う必要をなくしてくれます。

0グッド

0クリップ

投稿2017/05/29 23:41

編集2017/05/30 03:10

Rails 5.0.2、ruby 2.3.0にて開発しています。

通常の画面は問題なく動作しており、管理画面を作ろうと[ActiveAdmin](https://activeadmin.info/index.html)を入れたところ、以下のエラーuninitialized constant User::Messages が出てしまいました。

Stack Overflow等を見ると、modelの関連付けに誤りがある(例えば、単純にmodel名を複数形にしてしまっている、等)ことが原因であ流ことが多いそうなのですが、そもそも自分のmodelUserMessageの関連付けに問題あるとも思えず、誤りがお分かりの方ご指摘頂けますと幸甚です。

イメージ説明

ruby

1#UserModel 2class User < ApplicationRecord 3 # Include default devise modules. Others available are: 4 # :confirmable, :lockable, :timeoutable and :omniauthable 5 devise :database_authenticatable, :registerable, 6 :recoverable, :rememberable, :trackable, :validatable, 7 :omniauthable, :omniauth_providers => [:facebook] 8 9 has_one :adviser, dependent: :destroy 10 11 has_many :votes 12 has_many :voted_lessons, through: :votes, source: :lesson 13 14 has_many :comments 15 has_many :commented_lessons, through: :comments, source: :lesson 16 17 has_many :messages 18 has_many :outbound_messages, class_name: 'UserMessage', foreign_key: 'user_id' 19 has_many :inbound_messages, class_name: 'AdviserMessage', foreign_key: 'user_id' 20 21 mount_uploader :user_icon, UserIconUploader 22 23 validates :user_name, presence: true, uniqueness: { case_sensitive: :true }, 24 length: { minimum: 1, maximun: 20, allow_blank: true } 25 #, format: { with: /\A[A-Za-z]\w*\z/, allow_blank: false } 26 27 before_validation do 28 self.email_for_index = email.downcase if email 29 end 30 31 def self.new_with_session(params, session) 32 super.tap do |user| 33 if data = session["devise.facebook_data"] && session["devise.facebook_data"]["extra"]["raw_info"] 34 user.email = data["email"] if user.email.blank? 35 end 36 end 37 end 38 39 def self.from_omniauth(auth) 40 where(provider: auth.provider, uid: auth.uid).first_or_create do |user| 41 user.email = auth.info.email 42 user.password = Devise.friendly_token[0,20] 43 user.given_name = auth.info.name 44 user.user_name = auth.info.name # assuming the user model has a name 45 user.image = auth.info.image # assuming the user model has an image 46 user.user_icon = user.image 47 # If you are using confirmable and the provider(s) you use validate emails, 48 # uncomment the line below to skip the confirmation emails. 49 # user.skip_confirmation! 50 end 51 end 52 53 def self.new_with_session(params, session) 54 super.tap do |user| 55 if data = session["devise.facebook_data"] && session["devise.facebook_data"]["extra"]["raw_info"] 56 user.email = data["email"] if user.email.blank? 57 end 58 end 59 end 60end

ruby

1#Message model、それを継承するAdviserMessageとApplicantMessage 2class AdviserMessage < Message 3 scope :unprocessed, -> { 4 where(status: 'new')} 5end 6 7class ApplicantMessage < Message 8 scope :unprocessed, -> { 9 where(status: 'new')} 10end 11 12class Message < ApplicationRecord 13 belongs_to :user, optional: true 14 belongs_to :lesson, optional: true 15 belongs_to :applicant, class_name: 'User', foreign_key: 'applicant_id', optional: true 16 has_one :adviser, through: :lesson, class_name: 'Adviser', foreign_key: 'adviser_id' 17 18 belongs_to :root, class_name: 'Message', foreign_key: 'root_id', optional: true 19 belongs_to :parent, class_name: 'Message', foreign_key: 'parent_id', optional: true 20 has_many :children, class_name: 'Message', foreign_key: 'parent_id' 21 22 validates :body, presence: true 23 validates :body, length: {maximum: 800, allow_blank: true } 24 25 before_create do 26 if parent 27 self.applicant = parent.applicant 28 self.root = parent.root || parent 29 end 30 end 31 32 before_validation do 33 if parent 34 self.root = parent.root || parent 35 self.applicant = parent.applicant 36 end 37 end 38end

ruby

1message, applicant message, adviser messageのテーブル 2applicant messageとadviser messageはmessageを継承しているのみです 3 4id :integer not null, primary key 5user_id :integer not null 6lesson_id :integer 7adviser_id :integer 8applicant_id :integer 9root_id :integer 10parent_id :integer 11type :string not null 12status :string default("new"), not null 13body :text 14last_reply_date :datetime 15created_at :datetime not null 16updated_at :datetime not null

User ModelCRUDにはaccounts_controllerを使用しています。

ruby

1class User::AccountsController < User::Base 2 #before_action :signin_required 3 #before_action :check_account 4 before_action :authenticate_user! 5 before_action :search_preparation 6 7 def show 8 @user = current_user 9 end 10 11 def edit 12 @user = current_user 13 end 14 15 def update 16 @user = current_user 17 @user.assign_attributes(user_params) 18 if @user.save 19 redirect_to :user_account, notice: "更新しました。" 20 else 21 render "edit" 22 end 23 end 24 25 def icon_edit 26 @user = current_user 27 end 28 29 private def user_params 30 attrs = [ 31 :email, :family_name, :given_name, :family_name_kana, :given_name_kana, 32 :user_name, :address, :user_icon, :remove_user_icon 33 ] 34 params.require(:user).permit(attrs) 35 end 36end 37

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

moke

2017/05/30 01:46

ApplicantMessage < Messageはどう記述してますか?type,columnにclass名ですか?あとUserControllerも見せてください。
innjera

2017/05/30 02:24

コメント有難う御座います!本文に追記しました。
innjera

2017/05/30 03:09

すいません、そもそもユーザーコントローラーは作ったものの、使用していなかったので削除しました。User ModelのCRUDはaccounts_controllerにて行なっています
guest

回答1

0

自己解決

User Modelから以下を削除したら動きました、、、
has_many :outbound_messages, class_name: 'UserMessage', foreign_key: 'user_id'
has_many :inbound_messages, class_name: 'AdviserMessage', foreign_key: 'user_id'

投稿2017/06/13 13:16

innjera

総合スコア132

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問