3

这个问题不仅仅是一个问题,因此将其分解为更易于管理的部分:Rails Engines - simple possible engine to (1) add a model and (2) add the association in the containing class

我正在测试构建一个 Rails 引擎,我很好奇是否可以在托管/容器应用程序中为特定模型添加关联。

托管应用程序有一个用户模型类(是的,这永远不会改变),我的引擎称为 abc,我的引擎中有一个模型称为帖子(所以 Abc::Post 和表是 abc_posts)。我想将此关联添加到主应用程序的 User 类中。作为一个简单的尝试,我在我的引擎中创建了:

#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

通过 rails 控制台,我能够在表中创建记录(简单部分),但 User 类不知道关联。关于如何完成这项工作的任何想法?

提前谢谢

编辑 1

我尝试使用 forem 中使用的装饰器 gem(请参阅下面的评论)并拥有此文件:

#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"

但他似乎没有工作。不确定这是否是正确的策略,或者语法是否正确。

4

1 回答 1

1

这应该可以完成工作 - 指定关系的类:

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

嗯,我创建了一个例子,它确实有效......

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

具有 Child 类的模块在 model/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 
于 2013-05-02T19:38:26.587 回答