2012-08-01 102 views
1

我试图来从与客户相关联,但图书的信息似乎是中间的关联没有工作的Rails协会HAS_MANY

这里我的模型

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 

领域都有遵循

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

时,取我的高清编辑的信息,下面是全成

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

下似乎未能

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

有什么我错过?

我也尝试通过为book_manager控制器和视图创建索引进行验证,并且没有任何内容出现,它看起来是空的。我的方式创建的书是为遵循

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 

我已经更新我的关系,但仍然没有工作

的想法是一直跟随

一位顾客更新了状态,其中包含许多子部分,如

-Phone 
-Book 
-Interest 

Und呃,我应该看看是否有与客户有关的空白书,如果有的话,然后展示最后一本书。如果不是,那么客户看到空白,并可以创建一个新的

书籍经理只是在那里保持关系,也因为我想保留数据,并且我想让用户确定我们是否将这些数据显示给每个人否则在网站中。

+0

'@book = current_customer.books.build(PARAMS [:书])'我认为这是不正确。客户has_many book_managers,因此,Rails在建立新书时不知道你想使用哪一个。明确地将其分配给特定的'book_manager'可能有助于 – 2012-08-01 15:56:22

+0

哦..等一本书只与一位图书管理员关联,我可能需要改变我的关系 – Jseb 2012-08-01 15:58:27

+0

从客户界面的角度开始您的思考过程。他们可能会访问图书管理员页面并看到多位经理。当他们打开经理时,他们会看到多本书。对我来说,对于一个客户来说,“拥有”图书管理员似乎是最自然的事情,每个图书管理员都会拥有“自己的”图书你的案例中的“拥有”将转化为“所有者”一方的“has_many”和“ownee”一方的“belongs_to”。 – Kelvin 2012-08-01 18:31:43

回答

0

这是我的建议。我在sqlite3(内存中的数据库)中只是为了演示目的。

连接(在轨,使用替代的database.yml):

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

设置类:

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 

创建模式(这是在这里只是为了显示列在轨,使用迁移):

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 

创建记录

cust = Customer.new :email => '[email protected]' 
cust.book_managers.build :visible => true 
cust.book_managers.first.books.build :description => 'the odyssey' 
cust.save! 

检索

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

我不确定您的意思是:def customer book_manager.customers end – Jseb 2012-08-01 19:12:31

+0

我不明白您在书籍模型中的def客户的含义。这是做什么的?我从来没有见过这个在rails教程 – Jseb 2012-08-01 19:15:16

+0

@Jseb我正在定义一个名为'customer'的Book实例方法。当您拥有图书对象时,您可以调用'book.customer'来获取拥有该图书的客户对象。我强烈建议通过一个红宝石教程,否则你不会得到很远的轨道。 [这是一个](http://pine.fm/LearnToProgram/?Chapter=09),它解释了类中的实例方法。 – Kelvin 2012-08-01 19:18:55