Rails引擎 - 是否可以在像Forem这样的容器模型中添加模型的关联

时间:2013-05-02 19:08:42

标签: ruby-on-rails ruby-on-rails-3.2 rails-engines

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

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

托管应用程序有一个用户模型类(是的,这永远不会chnage),我的引擎被称为abc,我的引擎中有一个名为posts的模型(所以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控制台,我能够在表格中创建记录(简单部分),但是用户类并不知道关联。关于如何完成这项工作的任何想法?

事先提前

编辑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"

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

1 个答案:

答案 0 :(得分: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