3

I have a user#show view and also a membership#show view.

The users#show view has the path

/users/john-mcenroe

The membership#show view will have the path

/org-name/memberships/john-mcenroe

in other words, the user#show and org#show view both have identical slugs

For the sake of lookup speed I assume the best thing to do is to mirror the slug column from user on the membership model too. To that end I've created a user observer that updates the membership whenever the user's slug changes.

Is an observer the best way to echo de-normalised data across DB tables? Is there a native rails way to do this instead (an equivalent to counter_cache for isntance)?

4

1 回答 1

0

如果用户和会员之间存在关联,则可以使用“委托”方法。

例如:

class Membership
    belongs_to :user
    delegate :slug, :to => :user
end

在这种情况下,“slug”存储在用户模型中,您不需要将数据复制到成员模型中。

于 2012-11-23T18:00:07.173 回答