Question


I need your help to plan my Idea.

First I show you what I want to release:
![enter image description here][1]
There is a html-table which should have a content as above (Header-Date, title, time, location). As you can see, it's a schedule.

Model:
There is a user-model, which contains a schedule-column as an array, because I dont want to use the relationship behavior. So I just want to save the schedule-model-name in this array.
Furthermore there is a schedule-model, which contains date, title, time, location and a user-array for the access.

MESSAGE to the RoR-experts: Thanks for the belongs_to-suggestion, BUT I dont get it and I have to explain my decision at my oral exam, so that I just want to use, what I really understand.

Controller:
Handles the access to the schedule-model, e.g. @all.
I think that won't any problem to me.

View:
This is the point, where I need your help.
I googled something like "dynamic table content" and found this:
[Generate an HTML table from an array of hashes in Ruby][2], BUT I don't really understand any of their answers..

During typing this question, I realised that I should do more than @allin the Controller.
It's also important to have more than one appointment at a day.
If the time is similar or the same the appointments should shown in parallel.
If the time isn't similar the appointments should shown among themselves. (this isnt shown in the picture)

I also hope that there is someone who'll take his time to help.

Was it helpful?

Solution

You'd be best setting it up like this:

#app/models/user.rb
Class User < ActiveRecord::Base
    has_many :schedules
end

#app/models/schedule.rb
Class Schedule < ActiveRecord::Base
    belongs_to :user
end

Then your tables could be constructed as such:

#schedules
id | user_id | titel | location | time | created_at | updated_at

This means that you can do this:

#config/routes.rb
resources :users, only: :index

#app/controllers/users_controller.rb
def index
   @schedules = current_user.schedules
end

#app/views/users/index.html.erb
<%= for schedule in @schedules do %>
    <strong><%= schedule.titel %></strong>
    <%= schedule.time %>
    <%= schedule.location %>
<% end %>

This will output a list of schedules / appointments for your user. In order to get the grouped effect, you'll have to use the likes of group_by in your controller

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top