gpt4 book ai didi

ruby-on-rails - rails 数据库 :seed not populating postgresql on Ubuntu 16. 04

转载 作者:行者123 更新时间:2023-12-04 19:12:12 25 4
gpt4 key购买 nike

我有一个在 Ubuntu 16.04 服务器上运行的 Rails 应用程序。据我所知,似乎一切都设置正确

  • 我创建了一个名为“rails”的 psql 用户
  • 我做了myapp_name_production + _development & _test所有者设置为 rails (我的 Ubuntu 用户)
  • 我跑了rails db:createrails db:migrate
  • 我跑了rails db:seed但它只是在返回新命令行之前延迟了一会儿。

  • 我配置了我的 database.yml像这样:
    default: &default
    adapter: postgresql
    encoding: unicode
    pool: 5
    timeout: 5000
    host: localhost
    username: postgres
    password: mypassword

    development:
    <<: *default
    database: myapp_name_development

    production:
    <<: *default
    database: myapp_name_production

    我的 seeds.rb文件:
    news = BlogCategory.create name: "News"
    reviews = BlogCategory.create name: "Reviews"
    interviews = BlogCategory.create name: "Interviews"
    tutorials = BlogCategory.create name: "Tutorials"
    noCat = BlogCategory.create name: "No Category"

    subcat = BlogCategory.create name: "Audio Industry", parent_id: news.id
    subcat = BlogCategory.create name: "Game Audio", parent_id: news.id
    subcat = BlogCategory.create name: "Film Audio", parent_id: news.id

    subcat = BlogCategory.create name: "Software", parent_id: reviews.id
    subcat = BlogCategory.create name: "Hardware", parent_id: reviews.id

    subcat = BlogCategory.create name: "Sound Designers", parent_id: interviews.id
    subcat = BlogCategory.create name: "Composers", parent_id: interviews.id
    subcat = BlogCategory.create name: "Game Developers", parent_id: interviews.id
    subcat = BlogCategory.create name: "Voice Talent", parent_id: interviews.id

    subcat = BlogCategory.create name: "Sound Design", parent_id: tutorials.id
    subcat = BlogCategory.create name: "Composition", parent_id: tutorials.id
    subcat = BlogCategory.create name: "Implementation", parent_id: tutorials.id
    subcat = BlogCategory.create name: "Voice Acting", parent_id: tutorials.id

    Admin = User.create username: "Admin", first_name: "Admin", email: "[email@hotmail.com]", admin: true, password: "[password]", activated: true, slug: "admin"

    我不太确定我还需要做什么,我错过了什么吗?

    我还验证了这些表是通过执行以下操作创建的:
    $ psql -d myapp_name_production
    psql (9.5.13)
    Type "help" for help.

    myapp_name_production=> \d
    List of relations
    Schema | Name | Type | Owner
    --------+--------------------------+----------+-------
    public | ar_internal_metadata | table | rails
    public | blog_categories | table | rails
    public | blog_categories_id_seq | sequence | rails
    public | ckeditor_assets | table | rails
    public | ckeditor_assets_id_seq | sequence | rails
    public | comments | table | rails
    public | comments_id_seq | sequence | rails
    public | contacts | table | rails
    public | contacts_id_seq | sequence | rails
    public | friendly_id_slugs | table | rails
    public | friendly_id_slugs_id_seq | sequence | rails
    public | newsletters | table | rails
    public | newsletters_id_seq | sequence | rails
    public | packs | table | rails
    public | packs_id_seq | sequence | rails
    public | posts | table | rails
    public | posts_id_seq | sequence | rails
    public | purchases | table | rails
    public | purchases_id_seq | sequence | rails
    public | schema_migrations | table | rails
    public | users | table | rails
    public | users_id_seq | sequence | rails
    (22 rows)

    所有这些都在我的 localhost:3000 开发中的计算机(Windows)上运行。

    更新:

    我的 BlogCategory 模型:
    class BlogCategory < ApplicationRecord
    extend FriendlyId
    friendly_id :name, use: :slugged
    has_many :posts

    # This is called a self referential relation. This is where records in a table may point to other records in the same table.
    has_many :sub_categories, class_name: "BlogCategory", foreign_key: :parent_id
    has_many :sub_category_posts, through: :sub_categories, source: :posts
    belongs_to :parent, class_name: 'BlogCategory', foreign_key: :parent_id
    # This is a scope to load the top level categories and eager-load their posts, subcategories, and the subcategories' posts too.
    scope :top_level, -> { where(parent_id: nil).includes :posts, sub_categories: :posts }

    def should_generate_new_friendly_id?
    slug.nil? || name_changed?
    end

    end

    最佳答案

    你在你的模型中试过这个吗?:

    belongs_to :parent, class_name: 'BlogCategory', foreign_key: :parent_id, optional: true

    制作 belongs_to关联可选

    关于ruby-on-rails - rails 数据库 :seed not populating postgresql on Ubuntu 16. 04,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51573212/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com