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! -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 25 November 2010 20:10, Markus Zm <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.
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 -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 25 November 2010 21:18, Markus Zm <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: 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 << companySorry, 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 -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 26 November 2010 20:37, Markus Zm <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: 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 -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.