UserService 后端 Rails 应用程序上的三个模型:
class User < ActiveRecord::Base
has_many :services
has_many :members
has_many :groups, :through => :members
has_many :managed_groups, :class_name => "Group"
accepts_nested_attributes_for :managed_groups, :services, :groups
end
class Group < ActiveRecord::Base
has_many :members
has_many :users, :through => :members
belongs_to :manager, :class_name => "User", :foreign_key => :user_id
accepts_nested_attributes_for :users
end
class Service < ActiveRecord::Base
belongs_to :user
end
class Member < ActiveRecord::Base
belongs_to :group
belongs_to :user
end
单独的 Rails 前端中的三个 ActiveResource 模型
class User < ActiveResource::Base
self.site = "http://localhost:8801"
end
class Group < ActiveResource::Base
self.site = "http://localhost:8801"
end
class Service < ActiveResource::Base
self.site = "http://localhost:8801"
end
从前端,它可以做
Service.first.user_id = User.first
因为Service.user_id 是一个belongs_to……它是一个单一的属性。
但是我也做不到
User.first.groups << Group.first
或者
Group.first.users << User.first
什么都没有发生,因为 ActiveResource 只是产生 GET 请求。
为了完整起见,(在前端)执行以下操作也会失败。
u = User.first
g = Group.first
u.groups.push(g)
等等。我假设通过 users_controller::update 方法会起作用,但现在我质疑这是否可能。
有人用两个 ActiveResource 模型做过 :has_many, :through 吗?有什么方法可以影响 AR 模型的收集吗?我需要手动操作连接表吗?(从而创建一个members_controller?)