Rails的ActiveRecord的协会 [英] Rails ActiveRecord Association

查看:125
本文介绍了Rails的ActiveRecord的协会的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,这里是我的问题。我有3个不同的型号,人物,角色,客户端和存储。客户有很多商店,也可以有很多的人。商店有很多人。人有不同的角色。 1人可以在多个商店工作,它们可能会在各店铺不同的角色。

例如。乔可能是在一家商店的副经理和经理另一家商店。我希望能够做的是做一些像

 Store.find(1).people.find(1).roles 

(将返回拉正确的角色经理助理为例)或

 Store.find(2).people.find(1).roles 

(将返回经理为例)。这是可以做到的ActiveRecord的?

我已经创建了一个表:roles_people其定义如下:

CREATE_TABLE:roles_people,:ID =>假做| T |
      t.references:角色
      t.references:人
      t.references:专卖店
      t.references:客户端
结束

不过,我无法弄清楚如何让联想使用此表正常工作。任何人都可以点我朝着正确的方向?

感谢

解决方案

 类人
  belongs_to的:客户端
  的has_many:store_roles
结束

类角色
  的has_many:store_roles
结束

类StoreRole
  belongs_to的:角色
  belongs_to的:人
  belongs_to的:店面
结束

一流的客户端
  的has_many:专卖店
  的has_many:人
结束

一流的商店
  belongs_to的:客户端
  的has_many:store_roles
  的has_many:角色:通过=> :store_roles
结束
 

假设所有这些类从的ActiveRecord :: Base的继承;)

你将需要设置的迁移和数据库结构,以反映这些关系。对于每个 belongs_to的有一个:上表引用OBJECT_ID 字段中的相应表的id

您查询的是要需要看是这样的:

  Store.find(1).roles.find(:所有,:条件=> [?store_roles.person_id =1])
 

我可能会一个方法添加到专卖店模式,使这个更容易一些:

 高清roles_for(为person_id)
  roles.find(:所有,:条件=>store_roles.person_id =?,为person_id])
结束
 

此方式,您可以用找到的角色:

  Store.find(1).roles_for(1)
 

或者,更好的:

 高清self.roles_for(STORE_ID,为person_id)
  Role.find(:所有,:加入=>:store_roles,:条件=> [,STORE_ID,为person_idstore_roles.store_id = AND store_roles.person_id =?])
结束
 

这我们取景器更改为:

  Store.roles_for(1,1)
 

我会说,这最后一个方法是最为理想的,因为它会导致只有一个单一的查询,而其它各选项执行的两个查询到每个角色查询数据库(一个找到店里,一拿到对于为person_id的角色)。当然,如果你已经拥有的存储对象实例化,那么它是不是一个大问题。

希望这个答案就足够了:)

Okay, so here is my question. I have a 3 different models, People, Roles, Client, and Store. Clients have many Stores and can also have many people. Stores have many people. People have various roles. 1 Person can work at multiple stores, and they may have different roles at each store.

For example. Joe may be an assistant manager at one store and a manager at another store. What I would like to be able to do is pull the correct roles by doing something like

Store.find(1).people.find(1).roles

(would return 'assistant manager' for example) or

Store.find(2).people.find(1).roles

(would return 'manager' for example). Is this possible to do in ActiveRecord?

I've created a table :roles_people which has the following definition:

create_table :roles_people, :id => false do |t|
      t.references :role
      t.references :person
      t.references :store
      t.references :client
end

However i can't figure out how to get associations to work properly using this table. Can anyone point me in the right direction?

Thanks

解决方案

class People
  belongs_to :client
  has_many :store_roles
end

class Roles
  has_many :store_roles
end

class StoreRole
  belongs_to :role
  belongs_to :people
  belongs_to :store
end

class Client
  has_many :stores
  has_many :people
end

class Store
  belongs_to :client
  has_many :store_roles
  has_many :roles, :through => :store_roles
end

Assume that all of those classes inherit from ActiveRecord::Base ;)

You're going to need to setup the migration and database structure to mirror these relationships. For each belongs_to there is an :object_id field on the table reference the appropriate table's id.

Your query is going to need to look something like:

Store.find(1).roles.find(:all, :conditions => ["store_roles.person_id = ?", 1])

I would probably add a method to the store model to make this a little easier:

def roles_for(person_id)
  roles.find(:all, :conditions => ["store_roles.person_id = ?", person_id])
end

This way you can find the roles using:

Store.find(1).roles_for(1)

Or, better yet:

def self.roles_for(store_id, person_id)
  Role.find(:all, :joins => :store_roles, :conditions => ["store_roles.store_id = ? AND store_roles.person_id = ?", store_id, person_id])
end

Which changes our finder to:

Store.roles_for(1, 1)

I would say that this last method is the most ideal since it causes only a single query, while each of the other options execute two queries to the database per role look-up (one to find the store, and one to get the roles for a person_id). Of course if you already have the Store object instantiated then it's not a big deal.

Hopefully this answer was sufficient :)

这篇关于Rails的ActiveRecord的协会的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆