has_many :through - additional attribute

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. Can I access it from the user or company model?

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

Thanks!

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

Thanks! I will change the attribute name.

But do I have to go via user/company.responsibles or could I also access the additional 'type' attribute via via user.companies or company.users after the creation of a new record using

user.companies << company

Please don't top post, it makes it difficult to follow the thread. It is better to nsert your comments in appropriate places in previous post. Thanks.

Thanks! I will change the attribute name.

But do I have to go via user/company.responsibles or could I also access the additional 'type' attribute via via user.companies or company.users after the creation of a new record using

user.companies << company

Sorry, I don't understand exactly what you mean. Can you give an example of the code you would _like_ to enter, or a more detailed description?

Colin

My idea is that I have users, who are responsible for maintaining or for reviewing records relating to one or many companies. The other way round a company' records can be maintained or reviewed by many users. In the 'type' attribute I would like to store the role of the user as 'read-only' or 'write' flags.

I would like to define the authorization in a form that lists for a given user all existing companies and then the admin can select via radio-buttons for each company if the user should have 'no access', 'view' or 'write' access to the companies records. Submitting the form shall update the authorizations for all companies for that user. My intension was to store the type of authorization in the addtional datafield, but I don't know how to access it.

Markus

To repeat myself: Please don't top post, it makes it difficult to follow the thread. It is better to nsert your comments in appropriate places in previous post. Thanks.

My idea is that I have users, who are responsible for maintaining or for reviewing records relating to one or many companies. The other way round a company' records can be maintained or reviewed by many users. In the 'type' attribute I would like to store the role of the user as 'read-only' or 'write' flags.

I would like to define the authorization in a form that lists for a given user all existing companies and then the admin can select via radio-buttons for each company if the user should have 'no access', 'view' or 'write' access to the companies records. Submitting the form shall update the authorizations for all companies for that user. My intension was to store the type of authorization in the addtional datafield, but I don't know how to access it.

If you have a company then you can get all the responsibles for that company and hence the type field and the user. Similarly if you have a user then you have all his responsibles and for each one the type and company. So in a view you might have something like @users.responsibles.each do |responsible|   display responsible.company and responsible.type end

I think Responsibility might read better as a model name by the way.

Does that help?

Colin

Colin,

Does that help?

Thank you very much. I think I've got it. It works in the console, and now I will try to implement it in my application.

Regards, Markus

I have the same case as stated here but I would like to know if there is an elegant way to set the attribute in the join model. I kno the join model is created automatically using

user.companies << company

I was wondering if there is a way to set the attribute in the join model at the same time it is being created as shown above.