ログインしたユーザー(シェフ)がレシピにコメントを追加できるアプリを開発しましたが、開発中に最初にテストしてうまく機能しましたが、それをHerokuにデプロイするとアプリが正常に動作しないため、起こるかどうか確認するために「Herokuのは、レールコンソールを実行」、私は次の取得:なぜ私のレールアプリはHerokuで不要なプロパティを生成していますか?
上記のすべての@recipes = Recipe.first
=> #<Recipe id: 1, name: "RecipeOne", description: "Potato\r\nSausage",
created_at: "2017-09-16 00:58:19", updated_at: "2017-09-16 00:58:19", chef_id:
1>
@comment = @recipe.comments.build(description:'a delicious recipe')
=> #<Comment id: nil, description: "a delicious recipe", chef_id: nil,
recipe_id: 1, created_at: nil, updated_at: nil>
@chef = Chef.last
D, [2017-09-16T16:02:27.645527 #4] DEBUG -- : Chef Load (1.9ms) SELECT
"chefs".* FROM "chefs" ORDER BY "chefs"."id" DESC LIMIT $1 [["LIMIT", 1]]
=> #<Chef id: 2, name: "Wamba", email: "[email protected]", created_at: "2017-
09-16 01:13:36", updated_at: "2017-09-16 01:13:36", password_digest:
"$2a$10$WiIVsMk25EurnDaByNDNH.hWONivodBcvP8.cQJk8cM...", admin: false>
@comment.chef = @chef
=> #<Chef id: 2, name: "Wamba", email: "[email protected]", created_at: "2017-
09-16 01:13:36", updated_at: "2017-09-16 01:13:36", password_digest:
"$2a$6786544310$WiIVsMk25EurnDaByNDNH67428.hWONiv8900odBcvP8.cQJk8cM...", admin: false>
は完璧に動作しますが、私はcomment.save @入力するとき、それは失敗します。
@comment.save
D, [2017-09-16T16:04:22.305141 #4] DEBUG -- : (7.4ms) BEGIN
D, [2017-09-16T16:04:22.354525 #4] DEBUG -- : (9.5ms) ROLLBACK
=> false
ので、 @recipeと@chefが有効であることを確認しました。@chefは有効ですが、@recipeは無効ですが、@commentは無効です。私は@comment
@comment.errors
=> #<ActiveModel::Errors:0x0000000288d770 @base=#<Comment id: nil,
description: "a delicious recipe", chef_id: 2, recipe_id: 1, created_at: nil,
updated_at: nil>, @messages={:article=>["must exist"]}, @details={:article=>
[{:error=>:blank}]}>
をチェックし、私は何であるか疑問に思う:記事、なぜ私は存在しない属性を検証する必要がありますか?私はschema.rbをチェックして、 'Heroku pg:psql'を実行して、:articleプロパティに関する情報を見つけることができません。私は確信しています:記事は私のモデルにはありません。
#schema.rb
ActiveRecord::Schema.define(version: 20170912043409) do
create_table "chefs", force: :cascade do |t|
t.string "name"
t.string "email"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "password_digest"
t.boolean "admin", default: false
end
create_table "comments", force: :cascade do |t|
t.text "description"
t.integer "chef_id"
t.integer "recipe_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "ingredients", force: :cascade do |t|
t.string "name"
end
create_table "recipe_ingredients", force: :cascade do |t|
t.integer "recipe_id"
t.integer "ingredient_id"
end
create_table "recipes", force: :cascade do |t|
t.string "name"
t.text "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "chef_id"
end
end
モデル:
#comment.rb
class Comment < ApplicationRecord
validates :description, presence: true, length: {minimum: 4, maximum: 140}
belongs_to :chef
belongs_to :recipe
validates :chef_id, presence: true
validates :recipe_id, presence: true
default_scope -> {order(updated_at: :desc)}
end
#recipe.rb
class Recipe < ApplicationRecord
validates :name, presence: true
validates :description, presence: true, length: {minimum: 5, maximum:500}
belongs_to :chef
validates :chef_id, presence: true
default_scope-> { order(updated_at: :desc) }
has_many :recipe_ingredients
has_many :ingredients, through: :recipe_ingredients
has_many :comments, dependent: :destroy
end
#chef.rb
class Chef < ApplicationRecord
before_save {self.email = email.downcase}
validates :name, presence: true,
length: {maximum: 30}
VALID_EMAIL_REGEX = /\A[\w+-.][email protected][a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true ,length:{maximum: 255},
format:{with: VALID_EMAIL_REGEX},
uniqueness:{case_sensitive: false}
has_many :recipes, dependent: :destroy
has_secure_password
validates :password, presence: true, length:{minimum: 5}, allow_nil: true
has_many :comments, dependent: :destroy
end
#recipe_ingredient.rb
class RecipeIngredient< ApplicationRecord
belongs_to :ingredient
belongs_to :recipe
end
#ingredient.rb
class Ingredient <ApplicationRecord
validates :name, presence: true, length: {minimum: 3, maximum: 25}
validates_uniqueness_of :name
has_many :recipe_ingredients
has_many :recipes, through: :recipe_ingredients
end
rails -v => Rails 5.1.3
ruby -v => ruby 2.4.0p0 (2016-12-24 revision 57164) [x86_64-darwin15]
はあなたが本当に何が起こるかのいずれかのアイデアを持っていますか?
検証エラーはスキーマではなくモデルから発生しています。多分あなたはモデルを見せることができます。 –
私はモデルを追加しました。 –
これで、この検証モデルをコメントモデルに追加できますか? 'validates:article、presence:false'または' validates:article_id、presence:false' –