On 3 August 2012 15:57, Martyn W. <[email protected]> wrote:
> 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 %>

What happens when you do this?  You have specified a simple WorkTicket
belongs_to :material so there is nothing complex about this.

>
> Forcing me to use:
>
>     <%= work_ticket.part.material.number %>
>
> Am I expecting the wrong behaviour, or am I using the wrong relationship
> pattern?

It appears you have two routes for getting to material, that is bad as
there is redundant information somewhere and there is a danger of the
two routes getting mismatched.

Colin

-- 
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 [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to