2013-05-02 43 views
3

這個問題是不是一個單一的問題,更是這樣分解成更易於管理部分:Rails Engines - simple possible engine to (1) add a model and (2) add the association in the containing classRails的引擎 - 是有可能的關聯添加到模型容器中的模型像FOREM確實

我測試了構建Rails引擎,並且很好奇我是否可以將關聯添加到託管/容器應用中的特定模型。

託管應用程序有一個用戶模型類(是的,這將永遠不會chnage)和我的引擎被稱爲abc,我有我的引擎中的一個模型稱爲職位(所以Abc :: Post和表是abc_posts)。我想添加到主應用程序中的這個關聯的用戶類。

#located in the engine at: abc/app/models/user.rb 

class User < ActiveRecord::Base 
    has_many :abc_posts 
end 

後處理文件:

#located in the engine at: abc/app/models/abc/post.rb 
module Abc 
    class Post < ActiveRecord::Base 
    attr_accessible :body, :header, :user_id 
    belongs_to :user 
    end 
end 

通過導軌控制檯,我能夠創建表中的記錄(容易的部分),但由於下降死簡單的嘗試,我在我的引擎創建用戶類不知道關聯。有關如何完成此任務的任何想法?

提前THX

編輯1

我已經使用在培訓就業部所使用的裝飾寶石(見下面的評論),並有此文件的嘗試:

#abc/app/decorators/lib/abc/user_class_decorator.rb 
Object.const_get(User).class_eval do 
    has_many :abc_posts, :class_name => "Abc::Post", :foreign_key => "user_id" 
end 

我有包括修飾者通過: lib/abc.rb

require "decorators" 

但他似乎沒有工作。不知道這是正確的策略還是語法甚至是正確的。

+0

你得到一些錯誤?你有沒有試過: 'has_many:abc_posts,class_name:「Abc :: Post」'? – Lucas 2013-05-02 19:37:02

+0

嗯..我確實嘗試過,只是重新測試;它似乎沒有工作。 – timpone 2013-05-02 19:46:04

+0

我覺得我需要得到這個用戶類的引用並添加這個方法。 https://github.com/radar/forem做了這樣的事情,但現在很多代碼對我來說都是不透明的。 – timpone 2013-05-02 19:49:42

回答

0

這應該做的工作 - 指定類的關係:

class User < ActiveRecord::Base 
    has_many :posts, :class_name => "Abc::Post" 
end 

嗯,我創建了一個例子,它的工作...

class Parent < ActiveRecord::Base 
    has_many :children, :class_name => "Abc::Child" 
end 

與類子模塊在模型/ abc中。

module Abc 
    class Child < ActiveRecord::Base 
    belongs_to :parent 
    end 
end 

這裏雜誌

1.9.3-p194 :001 > Parent.create(:name => 'Mr Daddy') 
(0.1ms) begin transaction 
SQL (9.4ms) INSERT INTO "parents" ("created_at", "name", "updated_at") VALUES (?, ?, ?) [["created_at", Fri, 03 May 2013 10:49:54 UTC +00:00], ["name", "Mr Daddy"], ["updated_at", Fri, 03 May 2013 10:49:54 UTC +00:00]] 
(1.9ms) commit transaction 
=> #<Parent id: 1, name: "Mr Daddy", created_at: "2013-05-03 10:49:54", updated_at: "2013-05-03 10:49:54"> 
1.9.3-p194 :002 > Abc::Child.create(:name => 'Sammy boy', :parent => Parent.first) 
Parent Load (0.3ms) SELECT "parents".* FROM "parents" ORDER BY "parents"."id" ASC LIMIT 1 
(0.1ms) begin transaction 
SQL (117.3ms) INSERT INTO "children" ("created_at", "name", "parent_id", "updated_at") VALUES (?, ?, ?, ?) [["created_at", Fri, 03 May 2013 10:49:58 UTC +00:00], ["name", "Sammy boy"], ["parent_id", 1], ["updated_at", Fri, 03 May 2013 10:49:58 UTC +00:00]] 
(2.1ms) commit transaction 
=> #<Abc::Child id: 1, name: "Sammy boy", parent_id: 1, created_at: "2013-05-03 10:49:58", updated_at: "2013-05-03 10:49:58"> 
1.9.3-p194 :003 > Abc::Child.create(:name => 'Milly girl', :parent => Parent.first) 
Parent Load (0.3ms) SELECT "parents".* FROM "parents" ORDER BY "parents"."id" ASC LIMIT 1 
(0.2ms) begin transaction 
SQL (0.8ms) INSERT INTO "children" ("created_at", "name", "parent_id", "updated_at") VALUES (?, ?, ?, ?) [["created_at", Fri, 03 May 2013 10:50:15 UTC +00:00], ["name", "Milly girl"], ["parent_id", 1], ["updated_at", Fri, 03 May 2013 10:50:15 UTC +00:00]] 
(2.7ms) commit transaction 
=> #<Abc::Child id: 2, name: "Milly girl", parent_id: 1, created_at: "2013-05-03 10:50:15", updated_at: "2013-05-03 10:50:15"> 
1.9.3-p194 :004 > Parent.first.children.first 
Parent Load (0.4ms) SELECT "parents".* FROM "parents" ORDER BY "parents"."id" ASC LIMIT 1 
Abc::Child Load (0.3ms) SELECT "children".* FROM "children" WHERE "children"."parent_id" = ? ORDER BY "children"."id" ASC LIMIT 1 [["parent_id", 1]] 
=> #<Abc::Child id: 1, name: "Sammy boy", parent_id: 1, created_at: "2013-05-03 10:49:58", updated_at: "2013-05-03 10:49:58"> 
1.9.3-p194 :005 > Parent.first.children.last 
Parent Load (0.5ms) SELECT "parents".* FROM "parents" ORDER BY "parents"."id" ASC LIMIT 1 
Abc::Child Load (0.4ms) SELECT "children".* FROM "children" WHERE "children"."parent_id" = ? ORDER BY "children"."id" DESC LIMIT 1 [["parent_id", 1]] 
=> #<Abc::Child id: 2, name: "Milly girl", parent_id: 1, created_at: "2013-05-03 10:50:15", updated_at: "2013-05-03 10:50:15"> 
1.9.3-p194 :006 > Parent.first.children.count 
Parent Load (0.3ms) SELECT "parents".* FROM "parents" ORDER BY "parents"."id" ASC LIMIT 1 
(0.3ms) SELECT COUNT(*) FROM "children" WHERE "children"."parent_id" = ? [["parent_id", 1]] 
=> 2 
+0

我曾嘗試過但沒有骰子。 – timpone 2013-05-02 19:46:36