从Rails中的多个表中提取数据

时间:2017-11-15 22:59:59

标签: sql ruby-on-rails ruby database ruby-on-rails-5

我是Ruby on Rails的新手,我正在努力建立一个图书注册网站。一切都很好,除了类别部分。当用户为其图书分配类别时,我的数据库会复制book_categories.id并将其放入book.book_categories_id。该网站还有一个个人资料页面,您可以在其中查看用户的书籍。我的问题是显示category.name,我搜索了很多类似的问题,但我找不到正确的答案。

这是我的图书管理员:

before_action :set_book, except: [:index, :new, :create]
before_action :authenticate_user!, except: [:show]

def show
  @photos = @book.photos
end

def index
  @books = current_user.books
end

def new
  @book = current_user.books.build
end

def create
  @book = current_user.books.build(books_params)
  if @book.save
    redirect_to listing_book_path(@book), notice: "Saved."
  else
    flash[:alert] = "Failed."
    render :new
  end
end

private 
  def set_book
    @book = Book.find(params[:id])
  end

  def book_params
    params.require(:book).permit(:book_categories_id, :book_name, :summary, :address, :price, :company_name)
  end

只要我明白,我必须允许我的控制器访问我的类别表,但我不知道如何。此外,我的所有类别都存储在seed.rb

这是我的BookCategory.rb型号:

class BookCategory < ActiveRecord::Base
  has_many :books, :foreign_key => :book_categories_id
end

Book.rb型号:

class Book < ApplicationRecord
  belongs_to :user, :foreign_key => 'user_id'
  has_many :photos, dependent: :delete_all

  validates :book_name, presence: true
  validates :book_categories_id, presence: true

  def cover_photo
    if self.photos.length > 0
      self.photos[0].image.url
    else
      "default/image-default.jpg"
    end
  end

end

我的schema.rb:

create_table "book_categories", force: :cascade do |t|
  t.string "name"
  t.string "subcategory"
  t.text "description"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
end

create_table "book", force: :cascade do |t|
  t.string "book_name"
  t.text "summary"
  t.string "address"
  t.decimal "price", precision: 8, scale: 2
  t.boolean "active"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
  t.integer "user_id"
  t.float "latitude"
  t.float "longitude"
  t.string "company_name"
  t.integer "book_categories_id"
  t.index ["book_categories_id"], name: "index_books_on_book_categories_id"
end

通常,当我输入@book_categories.name时,我收到错误:

undefined method `name' for nil:NilClass

最后,它显示Parameters: {"id"=>"15"},而category.id则不同,而是通过book.id

我做错了什么?

2 个答案:

答案 0 :(得分:1)

首先修复您的命名问题。我不知道书架如何在架构中被称为“书”而不是“书籍”,但你可能必须首先修复它。

然后除了数据库表名以外,类别应该是单数。您需要进行迁移以修复books表中的外键及其索引,如下所示:

class FixCategoryNaming < ActiveRecord::Migration
  def change
    remove_column :books, :book_categories_id
    add_reference :books, :book_category, index: true, foreign_key: true
  end
end

然后运行rails db:migrate并检查架构是否正常

然后在BookCategory类中将行更改为

has_many :books

您需要将此行添加到Book

belongs_to :book_category

并将book_category的验证更改为此

validates :book_category_id, presence: true

然后在图书索引视图中,您有一个@books变量。您可以获取每本书的书名和类别名称,如下所示:

<% @books.each do |book| %>
  <p><%= book.book_name %></p>
  <p><%= book.book_categories.name %></p>
<% end %>

答案 1 :(得分:-1)

我认为您需要设置为onPostExecute关系。这是一个开始http://guides.rubyonrails.org/association_basics.html#the-has-many-through-association

的好地方