Ruby on Rails Friday, August 3, 2012

I have a 'has many through' relationship in my models. I am trying to
access objects from either side of this relationship, with mixed
results.Here are my models:

class Material < ActiveRecord::Base
attr_accessible :description, :number
has_many :parts
has_many :work_tickets, :through => :parts
end

class Part < ActiveRecord::Base
belongs_to :material
attr_accessible :description, :number, :yield, :material_id
has_many :work_tickets
has_many :operations, :dependent => :destroy
end

class WorkTicket < ActiveRecord::Base
belongs_to :part
belongs_to :material
attr_accessible :number, :yield, :opened, :closed, :part_id
has_many :works, :dependent => :destroy
end

I can access the work_tickets from the material with:

@work_tickets = @material.work_tickets

But, cannot access material from work_ticket:

<%= work_ticket.material.number %>

Forcing me to use:

<%= work_ticket.part.material.number %>

Am I expecting the wrong behaviour, or am I using the wrong relationship
pattern?

--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment