简体   繁体   中英

Model style for Ruby on Rails application

So currently my application has a model called 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

    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. Then I associated them with a has_many :through relationship, but I'm stuck onto which way is the best.

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.

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:

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.

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. Start with a single role per user to keep it simple. See: 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

I would also consider changing the name of the vendor_members table since one of the types is also members. Maybe something like vendor_relationships

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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