my issue is when I am creating a user in the rails console then it is not created. You can also check the below code:
This is my console:
Loading development environment (Rails 7.0.4)
irb(main):001:0> User.create(name: 'Tom', photo: 'https://unsplash.com/photos/F_
-0BxGuVvo', bio: 'Teacher from Mexico.')
=>
#<User:0x00007fb4e899aa80
id: nil,
name: "Tom",
photo: "https://unsplash.com/photos/F_-0BxGuVvo",
bio: "Teacher from Mexico.",
posts_counter: nil,
created_at: nil,
updated_at: nil>
These are my schema:
create_table "comments", force: :cascade do |t|
t.string "text"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.bigint "post_id"
t.bigint "author_id"
t.index ["author_id"], name: "index_comments_on_author_id"
t.index ["post_id"], name: "index_comments_on_post_id"
end
create_table "likes", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.bigint "post_id"
t.bigint "author_id"
t.index ["author_id"], name: "index_likes_on_author_id"
t.index ["post_id"], name: "index_likes_on_post_id"
end
create_table "posts", force: :cascade do |t|
t.string "title"
t.string "text"
t.integer "comments_counter"
t.integer "likes_counter"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.bigint "author_id"
t.index ["author_id"], name: "index_posts_on_author_id"
end
create_table "users", force: :cascade do |t|
t.string "name"
t.string "photo"
t.string "bio"
t.integer "posts_counter"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_foreign_key "comments", "posts"
add_foreign_key "comments", "users", column: "author_id"
add_foreign_key "likes", "posts"
add_foreign_key "likes", "users", column: "author_id"
add_foreign_key "posts", "users", column: "author_id"
This is my user model:
class User < ApplicationRecord
has_many :likes, foreign_key: "author_id"
has_many :comments, foreign_key: "author_id"
has_many :posts, foreign_key: "author_id"
validates :name, presence: true
validates :posts_counter, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
def most_recent_posts
posts.order(created_at: :desc).limit(3)
end
end
Thank you in advance.
You can check the above code and if anyone solves this problem then please help me.
CodePudding user response:
Your user model is validating posts_counter
, but you aren't providing one.
So User.create
is failing on this validation. You should be able to replicate this output:
Loading development environment (Rails 7.0.4)
irb(main):001:0> user = User.create(name: 'Tom', photo: 'https://unsplash.com/photos/F_
-0BxGuVvo', bio: 'Teacher from Mexico.')
=>
#<User:0x00007fb4e899aa80
id: nil,
name: "Tom",
photo: "https://unsplash.com/photos/F_-0BxGuVvo",
bio: "Teacher from Mexico.",
posts_counter: nil,
created_at: nil,
updated_at: nil>
> user.errors.messages
# will say something like "posts_counter must be greater than or equal to 0"
If you want posts_counter
to be optional, you must explicitly declare this:
validates :posts_counter, numericality: { only_integer: true, greater_than_or_equal_to: 0 }, allow_nil: true
If you want, you could instead change your migration to set a default value:
create_table :users do |t|
...
t.integer :posts_counter, default: 0, null: false
...
end
CodePudding user response:
As you have a validation defined for the posts_counter
attribute, you should provide a value:
posts_counter: 0
Or you can "relax" your validation to allow blank values:
validates :posts_counter,
numericality: { only_integer: true, greater_than_or_equal_to: 0 },
allow_blank: true
Don't forget you can check the object errors by:
user = User.create(...)
user.errors.full_messages