View association object - ruby-on-rails

Hi i have a table that is called contractors this is acting as the users model for logging into the system. I have a second table called employees. I have created the relationship between the two tables. contractors has_many employees and employees belong_to contractor.
The employee table has a field for contractor id as a foreign key.
When the contractor logs into how can i set the view to only show him the employees that belong to him
Thanks in advance!

I don't know how your authentication works, but sou should have something like current_user helper which retrieves the currently logged in user from session. Devise gem, for example, creates it automatically.
#employees = current_user.employees

Related

Changing the ID of a new object in Rails

So I have a Customer model in my Rails app and whenever a new customer is created I am getting a number as input from the user and I want that number to become the id of the customer on my database instead of the default 1, 2, 3, and so on. Is it possible to do that? If so, how?
EDIT: Let me be a more clear about what I want: I have a Customer and A Brand model. A customer has_many brands and a brand belongs_to a customer. Whenever the user creates a new brand, I want to connect it to its customer using that number that the user entered for the customer. Right now, whenever the user creates a new brand, I ask him to enter the customer_id value to connect the brand to a customer, but that is the id number generated by Rails. How do I make that customer_id attribute refer to the specific number the user entered for a customer, instead of the customer id generated by Rails.
It's possible, but don't do that. Just allow Rails to manage the auto-generated ID itself, and add this number - whatever it is - as a separate attribute in your model and saved to a separate field in your database.
As smathy mentioned, you can do the following:
1.First, generate a migration that add your custom attribute
rails g migration AddCustomerIdToCustomers customer_id:integer
2.When user enter the id, you would want to do something like this:
#customer.customer_id=params[:customer_id]
#customer.save
3.Then you can retrieve the custom object as
#customer = Customer.find_by_customer_id(customer_id)
where customer_id is the id you want.
As the above answer suggests, it is better let Rails handle the id attribute, because it is used extensively in Rails to handle database relationships ActiveRecord queries, etc. You do not want the extra headache of modifying it yourself.

How do I set up my model associations for an conference registration app?

I've been playing around with writing a RoR 4.2 app that will allow the following behaviors:
Admin users to create a list of conference courses that will include topic, description, etc
Conference attendees can register with the website and then select the conference courses they'd like to attend.
Conference attendees should not be able to create conference courses, only to select from a list of courses listed in the database and to select them to attend. The must be a way to associate many users with the same course.
When a user selects a course, the seating capacity for that course should be decremented (the details of this aspect of the solution are a secondary requirement at the time)
For the sake of discussion, let's say I am using Devise with user roles all figured out and configured. So I have one user.rb model/table.
I also created a model/table called course.rb.
I created a join model/table called course_selection.rb.
I configured these tables using the has_many through associations with the join table belonging to each like so:
class User < ActiveRecord::Base
has_many :course_selections
has_many :courses, :through => :course_selections
end
class Course < ActiveRecord::Base
has_many :course_selections
has_many :users, :through => :course_selections
end
class CourseSelection < ActiveRecord::Base
belongs_to :user
belongs_to :course
end
I have this set up and am able to create new users, but the users with non-admin roles are able to create courses (but I haven't set up any kind of role based exclusions for these users to prevent them from creating courses). When I call a RESTful route such as
http://localhost:3000/users/3/courses
I am expecting to see only a list of courses associated with this particular user. Instead I am seeing ALL the courses listed in the database.
My questions are:
Can the model associations strategy I've selected be used to accomplish the behavioral goals I've specified?
How would the controllers be set up so that a user's course selection is established, but that they are not using the new/create methods to actually create the course?
How would the forms be set up (I'm using simple_form) to allow the user to select from the list of existing courses?
Thanks for your help. I haven't found any tutorials that seem to address this challenge.
Can the model associations strategy I've selected be used to accomplish the behavioral goals I've specified?
Yes, those models are a fine start.
How would the controllers be set up so that a user's course selection is established, but that they are not using the new/create methods to actually create the course?
The typical Rails REST-like way is to have a controller CourseSelectionsController that manages the association.
Suppose you have a view page where a user can select a course. The view would submit the data to the CourseSelectionsController, with params user_id and course_id. The controller then creates the CourseSelection model, which associates the user and course.
The CourseSelectionsController would have typical actions for "create", "read", "delete". If the course selection also has other data, such as a price, or time slot, etc. then there can be an action for "update".
By the way, in your kind of application, the middle model is often called Enrollment instead of CourseSelection, and the REST controller is called EnrollmentsController.
How would the forms be set up (I'm using simple_form) to allow the user to select from the list of existing courses?
Typically in app/views/course_selections. The form is based on CourseSelections (not Users, and not Courses). The form shows a list of courses. When the user submits the form, it does a PUT (or POST) to CourseSelectionsController.
If you want the form to be able to handle multiple courses, one way is to use AJAX as each course is selected or unselected. Another way is to use multiple select fields, and make a controller method that can iterate on all the selected/unselected courses.

Rails 4 Devise and STI

I read the two posts here and here, but still have trouble figuring out how everything is tied together. Basically I have 2 types of users, Trainers and Clients. They share some common attributes (email, phone, first name, last name, etc), but they will also have some custom attributes.
Assuming STI is the way to go, I would have 3 models:
User (devise)
Trainer (inherits from User)
Client (inherits from User)
When the user signs up, they should be able to use the same form and just select from a drop down if they're a trainer or client. Once the form is submitted how do I go about specifying the type of user that has just been created? Do I need logic in the controller to check the user type, and then run Trainer.create() or Client.create()?

Devise with multiple models

How do I setup devise with more than one model?
I've already tried using rolify and cancan to setup separate roles in my database, but each role has a different way to authenticate themselves to login. For example, a student will have a student_number, and a lecturer will have a username but no student_number.
Also there is a bunch of other attributes a lecturer won't have that a student will and vice versa.
I'm new to rails 4.
It looks like classes and inheritance can come handy in this case.
What about defining a User mode and the let Student and Lecturer inherit from that class?
class Student < User
# student's peculiar attributes
end
class Lecturer < User
# lecturer's peculiar attributes
end
Then you can have two separate controllers and corresponding views. The the login page might have two links to the proper login pages.
I resolved this issue using a User model and a "has_one" Profile model attached to User, all users log in using the same table but devise load another data saved in Profile.
Other option is use just User model and left student_number empty when the user is not an student.

Tables and subtables relation in Active Record

I have a all users table and the users can be of type admin, coordinator or member.
I need another table admin which needs to look at the all users table and check for which users admin==true. If it is true it is in the admin table with other fields unique to admins.
How would I do this using Active Record?
Not sure why you need another table. You can easily get all the admin users with something like this:
admins = User.where(:admin => true)
This will give you an ActiveRecord::Relation which should suffice instead of actually another table.

Resources