Multiple associations from single table rails -
in attempt expand on this guide creating multiple associations same table came following user class
class user < activerecord::base has_secure_password validates_uniqueness_of :email validates_presence_of :email validates_presence_of :name belongs_to :role has_many :clients, foreign_key: 'rep_id' has_many :submitted_jobs, class_name: 'workorder', through: :clients has_many :processing_tasks, class_name: 'workorder', foreign_key: 'processor_id' def has_role?(role_sym) role.name.underscore.to_sym == role_sym end end
my goal able refer submitted jobs , processing tasks separately depending on type of user. far processing tasks part works expected , far can rep workorder, when attempt rep.submitted_jobs following error:
nomethoderror: undefined method `to_sym' nil:nilclass /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/activerecord-4.1.6/lib/active_record/reflection.rb:100:in `_reflect_on_association' ect...
clearly has_many through relationship works differently i'm expecting, i'm not quite sure call type of relationship i'm @ of loss for. worth noting that
rspec.describe user, type: :model {should validate_uniqueness_of(:email)} {should validate_presence_of(:name)} {should belong_to(:role)} {should have_many(:submitted_jobs)} {should have_many(:processing_tasks)} end
all pass
edit:
class client < activerecord::base has_many :contacts has_many :addresses, through: :contacts has_many :permits has_many :work_orders validates :clientnumber, format: { with: /\a\d{3}\z/ }, length: { is: 3 } belongs_to :rep, class_name: 'user' belongs_to :default_processor, class_name: 'user' end
edit 2: work order associations
class workorder < activerecord::base belongs_to :client belongs_to :project_type belongs_to :status belongs_to :labels belongs_to :contact belongs_to :processor, class_name: 'user' has_one :rep, through: :client, class_name: 'user' has_one :presort_information has_one :printing_instructions has_one :production_details ........ end
typical has_many_through_association like
to solve problem, write custom association, method give related work_orders
inside user model
def submitted_jobs workorder.joins(:client).where('clients.rep_id = ?', self.id) end
Comments
Post a Comment