How do I use elements of a record as wildcards when searching find_by?

Asked 2 years ago, Updated 2 years ago, 80 views

Is there a way to make an element of a record that is rails recognized as a wildcard or to implement something similar?

There is a post model that shows the article you posted.The post contains the author, title, and body that you wrote.
The post model has a one-to-many relationship with has_many:collaborations.
The collaboration model includes post_id and user_id, indicating who you share the post with to collaborate on editing and posting.

For example, post A is a post written by user A with one collaboration in it, and User B also has the right to write to post A if user B is registered in the collaboration user.

I wrote the following in the canability class:

 can: manage, Post do | post |
  Collaboration.find_by(user:user,post:post).present?
end

It seems to have been implemented so far.

I would like to include additional wildcard collaborations.Once a wildcard collaboration is registered in a post, all users have the right to co-edit.
"I would like users to be ""applicable to all users!"" at the age of find_by in the above code, but how should I implement it?"

I thought about making a subclass of collaboration, but I'm not sure.Please give me some advice!

ruby-on-rails ruby rails-activerecord

2022-09-30 19:18

1 Answers

I'm not very familiar with cancan, but if you define collaboration with user_id==nil as "collaboration for all users",

 can: manage, Post do | post |
  Collaboration.find_by(user:[user,nil], post:post).present?
end

So, I think it will work out well.


2022-09-30 19:18

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.