简体   繁体   English

Rails协会Has_Many

[英]Rails Association Has_Many

I am trying to fetch the information from books associated with a customer but seem that the middle association doesn't works 我正在尝试从与客户关联的书籍中获取信息,但似乎中间关联无效

Here my model 这是我的模特

Book
    has_one :book_manager
    has_one :customer, :through => :book_manager

Customer
    has_many :book_managers
    has_many :books, :through => :book_managers

Book_Manager
    belongs_to :customer
    belongs_to :book

The field are has follow 领域有

Book          Customer      book_manager
id            id            id
description   email         customer_id
              password      book_id
              first         visible
              last

When fetching the information in my def edit, the following is successfull 在def编辑中获取信息时,以下操作成功

@book = Book.first
@book = Book.last

The following seem to fails 以下似乎失败

@customer = Customer.find(params[:id])
@book = @customer.books.first
@book = @customer.books.order("created_at DESC").first

Is there something i miss? 我有什么想念的吗?

I also try to verify by creating an index for book_manager controller and view and nothing appears, its seems its empty. 我还尝试通过为book_manager控制器和视图创建索引来进行验证,但没有任何显示,它似乎是空的。 The way i created the books was as follow 我的创作方式如下

BookController BookController

def create
@book = current_customer.books.build(params[:book])
    if @book.save
        flash[:success] = "Book Created"
        redirect_to root_url
    else
        render 'customer/edit'
    end
end

I have updated my relationship but still doesn't to work 我已经更新了我的关系,但仍然无法正常工作

The idea is has follow 这个想法是跟随

A customer update his status, which consist of many subsection such has 客户更新其状态,其中包括许多小节

-Phone
-Book
-Interest

Under book I should see if there exist an empty book related to the customer, if so then showcase the last book. 在书本下,我应该查看是否存在与客户相关的空书,如果有,则展示最后一本书。 if not then customer see blank and can create a new one 如果不是,则客户看到空白并可以创建一个新的

The books managers is only there to maintain the relationship and also because i want to keep the data and i want to allow user to determine if we show this data to everyone else in the site or not. 图书经理只是在那里维护关系,还因为我想保留数据,并且想让用户确定是否向站点中的其他所有人显示此数据。

Here's my suggestion. 这是我的建议。 I'm doing this in sqlite3 (with in-memory database) just for demonstration purposes. 我在sqlite3(带有内存数据库)中进行此操作仅出于演示目的。

Connect (in rails, use the database.yml instead): 连接(在rails中,改用database.yml):

ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => ':memory:'

Setup classes: 设置类:

class Customer < ActiveRecord::Base
  has_many :book_managers
  has_many :books, :through => :book_managers
end

class BookManager < ActiveRecord::Base
  belongs_to :customer
  has_many :books
end

class Book < ActiveRecord::Base
  belongs_to :book_manager
  def customer
    book_manager.customer
  end
end

Create schema (This is here just to show the columns. In rails, use migrations): 创建架构(此处仅用于显示列。在rails中,使用迁移):

Book.connection.create_table(Book.table_name) do |t|
  t.string :description
  t.integer :book_manager_id
end
BookManager.connection.create_table(BookManager.table_name) do |t|
   t.boolean :visible
  t.integer :customer_id
end
Customer.connection.create_table(Customer.table_name) do |t|
  t.string :email
  t.string :password
  t.string :first
  t.string :last
end

Create a record 建立记录

cust = Customer.new :email => 'user@example.com'
cust.book_managers.build :visible => true
cust.book_managers.first.books.build :description => 'the odyssey'
cust.save!

Retrieve 取回

cust = Customer.find 1
# test the has_many :through
cust.books
# test the Book#customer method
Book.first.customer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM