Rails Engines - is it possible to add associations to the model in a container model like Forem does -
this question more single question breaking more managable pieces: rails engines - simple possible engine (1) add model , (2) add association in containing class
i testing out building rails engine , curious whether can add association specific model in hosting / container app.
the hosting app has user model class (yes, never chnage) , engine called abc , have model in engine called posts (so abc::post , table abc_posts). i'd add user class in main app association. drop dead simple try, created in engine:
#located in engine at: abc/app/models/user.rb class user < activerecord::base has_many :abc_posts end
the post file:
#located in 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
via rails console, able create records in table (easy part) user class doesn't know association. ideas on how done?
thx in advance
edit 1
i've tried using decorators gem used in forem (see comment below) , have file:
#abc/app/decorators/lib/abc/user_class_decorator.rb object.const_get(user).class_eval has_many :abc_posts, :class_name => "abc::post", :foreign_key => "user_id" end
i have included decorators via: lib/abc.rb
require "decorators"
but doesn't seem working. not sure if right strategy or whether syntax correct.
that should job - specify class relationship:
class user < activerecord::base has_many :posts, :class_name => "abc::post" end
hmmm, created example , work ...
class parent < activerecord::base has_many :children, :class_name => "abc::child" end
the module class child in model/abc.
module abc class child < activerecord::base belongs_to :parent end end
here journal
1.9.3-p194 :001 > parent.create(:name => 'mr daddy') (0.1ms) begin transaction sql (9.4ms) insert "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".* "parents" order "parents"."id" asc limit 1 (0.1ms) begin transaction sql (117.3ms) insert "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".* "parents" order "parents"."id" asc limit 1 (0.2ms) begin transaction sql (0.8ms) insert "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".* "parents" order "parents"."id" asc limit 1 abc::child load (0.3ms) select "children".* "children" "children"."parent_id" = ? order "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".* "parents" order "parents"."id" asc limit 1 abc::child load (0.4ms) select "children".* "children" "children"."parent_id" = ? order "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".* "parents" order "parents"."id" asc limit 1 (0.3ms) select count(*) "children" "children"."parent_id" = ? [["parent_id", 1]] => 2
Comments
Post a Comment