What is the best practice for associations and view restrictions? [closed] - ruby-on-rails

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I'm working on a rails app to teach myself associations. Its an app with Tutors and Students where Tutors create Students as Users and can then Tutors create "posts" for what was taught in that lesson to the specific Students page to show their progress. When the Student logs in they can only see their assigned progress and do nothing else.
I figured an option is having Students as "categories" so the Students can be filtered but what is the best practice?
It seems simple but I keep overthinking it

Generally you want to create the associations as you need them in your code later. So if a tutor has many students and many posts and a student has many posts and belongs to a tutor and so on.
In order to restrict it in the view, you can restrict that in the controller. So this is where you can filter the posts by the students then. So you could do student.posts (Beware, I am not super sure what your db structure/schema looks like).
And there are gems that handle authorization. Look at pundit (my favorite) or cancancan. With those you can specifiy for each controller action, who can see/do certain things.
Now judging from the little information you gave, you might also want to look into namespaced controllers because you probably have a posts#index for tutors and another one for students. Here you can get another layer of organisation by adding a so called namespace meaning you can have one posts controller under tutors and one under students. You can find more info here: https://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing
Let me know if anything is unclear!

Related

Proper database design and relationship for invoicing application [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
I am creating an invoice application. Does the following flow diagram is the proper & complete application?
Am I missing any model?
Is there any other better way to design?
Is there any option to reduce the number of tables?
In the current model, when a user adds new invoice - I need to interact with almost all the tables.
When a user edits the same invoice - Again I need to interact with all the mentioned tables.
Well it's hard to say, because only you know the specification of your application and what it should do and you are posting some concept not a design (eg. UML diagrams). But giving the information you provide:
Invoice have one contact. Would be better having issuer and contractor (two contacts) in case you want to issue an invoice from many clients.
I can answer that question when I see UML diagrams.
Why you want to reduce a number of tables? If you want to you can have one big table but that's not the point. Just keep your database desing normalized (3rd normal form) and don't care about number of tables.
On a must, I think you don't need tax and currency relations, you can move them to Invoice attributes - but once again, I don't have the whole picture.
No, you don't. When you issue a new invoice to the same customer and sell him the same items you just add records to two tables (invoices, invoice items). Anyway don't think about modyfing the database that way, you have the DB to work on it :)
As mentioned above - no.

User authentication and authorization for rails app [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Excuse my lack of knowledge,
Lately, I am learning rails and found it extremely easy and fun, but still there is alot to learn.
I made an application for managing a Car workshop:
Owners has many cars
cars has many visits
visits belong to a car and an owner.
I wanna have the ability for diffrent users of workshops to manage their businesses, what is the currect design in order to make visits and cars and owners belong to that specific user, is making all models belong to User enough? where can I read about this? managing access to dabases based on the logged in user?
I read about authentication, but its not answering my question, which is the best design for my needs, which is workshops managing only their data.
You could probably check Devise (https://github.com/plataformatec/devise) which is one of the most common ways of handling authentication
In the end, you will have a User model and then you can treat it as any other model, so for example if you only want to show a user's cars you could do something like current_user.cars and restrict certain controller actions to happen only if user_signed_in? and so on. I do recommend checking their documentation, since most use cases are explained easily.
Devise can be used to handle authentication and Pundit or CanCanCan can be used for authorization. I'd recommend looking at those, there are many tutorials which will answer your question for those.

Relating my models/tables? STI? Polymorphic? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I currently have three models: Business, Charity, and Organization. A business and charity are both types of organizations because they share many similar attributes (e.g. address, hours, website, etc.) however they each have their own unique attributes. How should I best handle the creation of the models in Active Record? Use STI or Polymorphism? Or should I break out each one into their own model with duplicate information and get rid of the Organization model?
You can use both, so it is not necessarily an either/or situation. Most importantly, however, is how you will structure your tables and the logic surrounding them, which depends on how you are going to query the data, and how these elements relate to one another.
There is not enough information above to give you very clear direction on what to use. However, if after reading:
Rails Guides
How and When to Use STI
... the solution isn't clear, here are some simple rules:
if the types of objects you are using mostly have the same attributes but have different business logic attached to them (ie, the behavior written into their Model classes), then STI is a good baseline idea, but
if they have the same logic as well, it may make sense just to create them as a single class with a "type" flag (but not the attribute type, since it is only for STI)
As far as polymorphism goes, it seems like these Models are all very similar and interchangeable -- polymorphism is more useful for relating unlike things (such as comments and photos) to another Model (such as a FB post). In that case, using either a single table or STI and relating based on the parent table may make more sense.

Add new column in user-database with controller of lecture [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
i have a little system with some users.
An user with admin-rights can create a new lecture.
Thats already working.
Now i want to solve the problem that users can sign up to lectures.
So i want to add a new column (boolean type) e.g. to lecture1 in my users database when i create a new lecture. So i can set the variable to true if an user sign up.
Is that a good idea or would it be better to have a database (e.g. signupstatus) with an exercise_id, an user_id. When there is an entry the user has already sign up to the lecture.
Or does anyone have an better idea?
Thanks :)
The table is always better. You will never want to create such columns(lecture1...) in User table, for each new lecture which to added in your system. Imagine the series of such columns [lecture1,lecture2,lecture3,lecturen...] => Totally unconventional and against the design principles.
What you want is a simple Many-to-Many relationship. A Lecture can be enrolled by many users; a User can subscribe to many lectures.
You should follow this guide to achieve it.
This will create a table - lectures_users and store lecture_id and user_id to maintain the relationship

Best practice for determining necessary controllers for simple ASP.NET MVC site? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I'm playing with the ASP.NET MVC Framework, trying to create a simple site.
My model is essentially:
-Questions
-Answers
-Categories
Since each Question must belong to a Category and each Answer must belong to a question - is it correct to split 'Category' into it's own controller?
What would be incorrect about creating a controller path of /Question/Category/List?
Also, if I wanted to create a search - do I then create a controller called 'Search' and use like so: /Search/Question/, /Search/Answer/? Or do I use '/Question/Search/'?
Thank you in advance for any insight.
Below is a read regarding grouping controllers
Grouping Conrollers
The above is not directly related to your question but may be useful.
Now regarding your question, I'll take it this way.. (This is just one way)..
My initial take on this...
-for Questions and Category
/Question/Ask
/Question/Edit/{id}
/Question/List
/Question/Search/{criteria}
/Category/Create
/Category/Edit/{id}
/Category/List
-for answer
/Question/Answer/{questionid}
/Question/Answer/Search/
/Question/Answer/List/Group/Question
The other approach is to separate the controller with respect to Admin and Visitor functionality. Put all the admin logic like create/update/ in one controller and split the remaining logic into other controllers. There may be +/- argument to this. But's that's an individuated opinion.
Note:
Category can exist independent of the question
A question is associated with one or more category
An answer belongs to a question and cannot exist independently.
*** The example came out of top of my head and may not be the best practice but may just give some points to ponder. :). Feel free to apply your ideas.

Resources