ruby on rails - Confused by has_many and belongs_to associations -


i'm little bit confused here. have 2 models: user ticket

  • a ticket belongs 1 user "reporter".
  • a ticket belongs 1 user "assigned".

  • a user has many tickets (twice ?)

so here i've got:

# table name: tickets # #  id             :integer         not null, primary key #  label          :string(255) #  content        :text #  reported_by_id :integer #  assigned_to_id :integer #  created_at     :datetime #  updated_at     :datetime # class ticket < activerecord::base   belongs_to :reported_by, :class_name => 'user'   belongs_to :assigned_to, :class_name => 'user' end  # table name: users # #  id         :integer         not null, primary key #  login      :string(255) #  password   :string(255) #  created_at :datetime #  updated_at :datetime # class user < activerecord::base   has_many :tickets, :class_name => 'ticket', :foreign_key => 'reported_by_id'   has_many :tickets, :class_name => 'ticket', :foreign_key => 'assigned_to_id' end 

i "auser.tickets" , user's tickets reported.

any ? thx !

you should differentiate names of has_many in user model:

 class user < activerecord::base    has_many :reported_by_tickets, :class_name => 'ticket', :foreign_key => 'reported_by_id'    has_many :assigned_to_tickets, :class_name => 'ticket', :foreign_key => 'assigned_to_id'  end 

now call

 @user.reported_by_tickets  @user.assigned_to_tickets 

otherwise, code looks right on target.


Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

php - How can I edit my code to echo the data of child's element where my search term was found in, in XMLReader? -