Ruby on Rails Thursday, November 25, 2010

On 25 November 2010 20:10, Markus Zm <lists@ruby-forum.com> wrote:
> Hi,
>
> I'm wondering how to write or read an additional attribute in the
> through table.
> Beside the foreign key, I have in the 'Responsible' table an additional
> data field 'type' to set the kind of 'responsibility' for the company.

Don't use 'type' it is a reserved word. Rails will think you are
doing STI. See http://wiki.rubyonrails.org/rails/pages/ReservedWords

> Can I access it from the user or company model?

You have to specify which of the responsibles for the user you want,
so for example
user.responsibles[0].type

Colin

>
> class User < ActiveRecord::Base
>  has_many :responsibles
>  has_many :companies, :through => :responsibles
> end
>
> class Company < ActiveRecord::Base
>  has_many :responsibles
>  has_many :users, :through => :responsibles
> end
>
> class Responsible < ActiveRecord::Base
>  # datafields: user_id, company_id, type:string
>  belongs_to :user
>  belongs_to :company
> end

--
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 this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment