ruby on rails - Reaching 'tickets' that belongs to 'projects' that a user owns -
in project management app i'm working on, i'm working on page managing tickets, want should contain of following:
- tickets user has created - tickets belongs projects user has created
the tricky part utilize right code in controller, need help. '@users_tickets'works fine, '@owned_projects'. however, lastly thing creating array contains of tickets belongs projects user owns, need help me (yes, understand poor seek each loop totally wrong way go here).
how can accomplish want?
tickets controller:
1. def manage 2. @users_tickets = ticket.where(:user_id => current_user.id) 3. @owned_projects = project.where(:user_id => current_user) 4. 5. @owned_projects.each |project| 6. @tickets_for_owned_projects = ticket.where(:project_id => project.id) 7. end 8. end
tables: tickets table:
project_id ticket_status_id user_id title description start_date end_date
projects table:
user_id title description start_date end_date
if you're using has_many
association, should simple as
class user < activerecord::base has_many :projects has_many :tickets has_many :project_tickets, through: :projects, class_name: 'ticket', source: :tickets #... end class project < activerecord::base has_many :tickets #... end # in tickets controller def manage @tickets = current_user.tickets @tickets_for_owned_projects = current_user.project_tickets end
upd: approach above should work. i'm literally falling asleep right , can't define wrong here. appreciate if looked it.
here's way around though.
class user < activerecord::base has_many :projects has_many :tickets def project_tickets result = [] self.projects.each |project| result << project.tickets end result.flatten end #... end
ruby-on-rails database ruby-on-rails-3 table
No comments:
Post a Comment