简体   繁体   English

Ruby on Rails应用程序的模型样式

[英]Model style for Ruby on Rails application

So currently my application has a model called Vendors. 因此,当前我的应用程序具有一个名为Vendors的模型。 I want each Vendor to have one or many owners, one or many members, and then people who track or follow the Vendor. 我希望每个供应商都有一个或多个所有者,一个或多个成员,然后是跟踪或关注该供应商的人员。 I'm trying to decide the best layout for this and I've messed around with a few things. 我正在尝试为此确定最佳布局,并且弄乱了一些东西。 All of the Owners, members, and followers would also be coming from my User model. 所有所有者,成员和关注者也将来自我的用户模型。 Therefore I'm thinking of making one association table with booleans as to whether the User is a follower, owner, or member. 因此,我正在考虑使用布尔值创建一个关联表,以确定用户是追随者,所有者还是成员。

For clarification - Owner and member would be people who work at the vendor 为了澄清-所有者和成员是在供应商工作的人

schema.db schema.db

    create_table "vendor_relationships", force: true do |t|
        t.integer  "user_id"
        t.integer  "vendor_id"
        t.boolean "owner"
        t.boolean "member"
        t.boolean "follower"
        t.datetime "created_at"
        t.datetime "updated_at"
    end

but I'm wondering if this is the best way to go about this when it comes to speed and/or quality of code. 但是我想知道这是否是解决速度和/或代码质量的最佳方法。 Previously I had created separate tables for vendor_owners, vendor_members, and vendor_followers. 以前,我已经为vendor_owners,vendor_members和vendor_followers创建了单独的表。 Then I associated them with a has_many :through relationship, but I'm stuck onto which way is the best. 然后,我将它们与has_many:through关系相关联,但是我被困在哪种方法最好。

They would all have separate logic, or rather separate permissions as to what they were allowed to view and edit with respect to the vendor. 他们都将具有单独的逻辑,或者就允许他们查看和编辑有关供应商的内容而言具有单独的权限。 I looking at cancan and pundit for role based authorization, but I didn't think it really applied for this situation. 我在寻找cancan和pundit来进行基于角色的授权,但是我不认为它真的适用于这种情况。

A role based authorization could work if I build the schema like this 如果我像这样构建架构,基于角色的授权可能会起作用

    create_table "vendor_relationships", force: true do |t|
        t.integer  "user_id"
        t.integer  "vendor_id"
        t.string "role"
        t.datetime "created_at"
        t.datetime "updated_at"
    end

And then check for possible user roles against the string but I'm not sure which of these is the better option. 然后针对该字符串检查可能的用户角色,但是我不确定哪个是更好的选择。

EDIT 1: 编辑1:

I'm going with the t.string "role" route, but I'm wondering how I can write the code into my model so that the associations with role = "owner" would be accessible by doing Vendor.owners... etc with Vendor.members and Vendor.followers. 我正在使用t.string“ role”路线,但我想知道如何将代码写入模型中,以便可以通过执行Vendor.owners等访问角色=“ owner”的关联与Vendor.members和Vendor.followers。

This is what my code looks like currently. 这是我的代码当前的样子。

           has_many :owners, through: :vendor_relationships,  class_name: "User", -> { where role: owner }

This for each variation - owners, members, followers. 每种版本-所有者,成员,关注者。 I've also tried using 我也尝试过使用

          has_many :owners, through: :vendor_relationships,  class_name: "User", conditions: => ['vendor_relationship.role = "owner"']

but everything is giving me syntax errors here. 但一切都给我语法错误。 Would appreciate some help thanks. 希望能有所帮助,谢谢。

I think you are on the right track. 我认为您在正确的轨道上。 I would do the following: 我将执行以下操作:

Model your owners, members, and followers all as User , but then give them each a role. 将您的所有者,成员和关注者全部建模为User ,然后给他们每个角色。 Start with a single role per user to keep it simple. 从每个用户的单一角色开始,以使其保持简单。 See: https://github.com/ryanb/cancan/wiki/Role-Based-Authorization 参见: https : //github.com/ryanb/cancan/wiki/Role-Based-Authorization

In the many-to-many joins table, I would remove the booleans and instead let your association handle that logic. 在多对多联接表中,我将删除布尔值,而是让您的关联处理该逻辑。

has_many :owners, through: :vendor_members, class_name: "User", conditions: {"users.role = 'owner'"}

Then you can just interact with owners/members/followers like so: owners = vendor.owners 然后,您可以像这样与所有者/成员/跟随者进行交互: owners = vendor.owners

I would also consider changing the name of the vendor_members table since one of the types is also members. 我还将考虑更改vendor_members表的名称,因为其中一种类型也是成员。 Maybe something like vendor_relationships 也许像vendor_relationships

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM