gpt4 book ai didi

ruby-on-rails - Rails Engines - 是否可以像 Forem 那样在容器模型中向模型添加关联

转载 作者:行者123 更新时间:2023-12-02 03:46:58 24 4
gpt4 key购买 nike

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

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

托管应用程序有一个用户模型类(是的,这永远不会改变),我的引擎称为 abc,我的引擎中有一个名为 posts 的模型(所以 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

通过 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

我通过以下方式包含了装饰器:库/abc.rb

require "decorators"

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

最佳答案

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

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

关于ruby-on-rails - Rails Engines - 是否可以像 Forem 那样在容器模型中向模型添加关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16345883/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com