Hello, I''m doing right now my first steps in Ruby on Rails and I''ve got a problem. Just some explanation: A project can have ressources (like an employee) and this ressource can have certain roles (e.g. a team member, or a team leader). As you can see in the attachment, I have a Table called Projects_Ressources. This Table has three relationships (belongs_to). One to Projects, one to Ressources and one to Roles. This is my model description for, role.rb: <code> class Role < ActiveRecord::Base has_many :projects_ressources has_many :ressources, :through => :projects_ressources has_many :permissions_roles has_many :permissions, :through => :permissions_roles end </code> project.rb: <code> class Project < ActiveRecord::Base has_many :projects_ressources has_many :ressources, :through => :projects_ressources end </> ressource.rb: <code> class Ressource < ActiveRecord::Base has_many :projects_ressources has_many :projects, :through => :projects_ressources has_many :roles, :through => :projects_ressources end </code> projects_ressources.rb: <code> class ProjectsRessource < ActiveRecord::Base belongs_to :ressource belongs_to :project belongs_to :role end </code> When I''m now in the console, I can get for example the project with id 1>> pro = Project.find(1)And can get all the ressources to this certain project:>> employee = pro.ressourcesBut I want also to know the roles of this ressources. How can I get now the roles of the ressources to this project with id 1? This doesn''t work:>> role = pro.rolesproduces this error in the console: <code> NoMethodError: undefined method `roles'' for #<Project:0x4dffdc0> from c:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record /attribute_methods.rb:260:in `method_missing'' from (irb):47 from :0 </code> Is the design of the relationship wrong or what am I missing? Attachments: http://www.ruby-forum.com/attachment/4360/erm.PNG -- 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.
As seen from above in Project class there is no relation ship defined for role so how can you expect the result from it. Add has_many :roles, :through => :projects_ressources and the required output will be produced. On Jan 7, 10:54 am, Sven Schleier <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hello, > I''m doing right now my first steps in Ruby on Rails and I''ve got a > problem. > Just some explanation: > A project can have ressources (like an employee) and this ressource can > have certain roles (e.g. a team member, or a team leader). > As you can see in the attachment, I have a Table called > Projects_Ressources. This Table has three relationships (belongs_to). > One to Projects, one to Ressources and one to Roles. > > This is my model description for, > > role.rb: > <code> > class Role < ActiveRecord::Base > has_many :projects_ressources > has_many :ressources, :through => :projects_ressources > has_many :permissions_roles > has_many :permissions, :through => :permissions_roles > end > </code> > > project.rb: > <code> > class Project < ActiveRecord::Base > has_many :projects_ressources > has_many :ressources, :through => :projects_ressources > end > </> > > ressource.rb: > <code> > class Ressource < ActiveRecord::Base > has_many :projects_ressources > has_many :projects, :through => :projects_ressources > has_many :roles, :through => :projects_ressources > end > </code> > > projects_ressources.rb: > <code> > class ProjectsRessource < ActiveRecord::Base > belongs_to :ressource > belongs_to :project > belongs_to :role > end > </code> > > When I''m now in the console, I can get for example the project with id 1 > > >> pro = Project.find(1) > > And can get all the ressources to this certain project: > > >> employee = pro.ressources > > But I want also to know the roles of this ressources. How can I get now > the roles of the ressources to this project with id 1? This doesn''t > work: > > >> role = pro.roles > > produces this error in the console: > > <code> > NoMethodError: undefined method `roles'' for #<Project:0x4dffdc0> > from > c:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record > /attribute_methods.rb:260:in `method_missing'' > from (irb):47 > from :0 > </code> > > Is the design of the relationship wrong or what am I missing? > > Attachments:http://www.ruby-forum.com/attachment/4360/erm.PNG > > -- > Posted viahttp://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.
Hi ! According to your relationships: resource :has_many => roles but you are using pro.roles. It means project should has many roles.Its totally wrong accordingly. First fetch all project_resources for a particular project. Eg. pr_resources = Project.find(1).project_resources pr_resources will be a array of pr_resources Now you can fetch any employee(resource) and its role associated with project id 1. for eg. for any project_resource: resource = pr_resource.resource role = pr_resource.role Thanks, -- ~ Ankur Gupta Company: www.innozon.com, Hyderabad On Jan 7, 10:54 am, Sven Schleier <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hello, > I''m doing right now my first steps in Ruby on Rails and I''ve got a > problem. > Just some explanation: > A project can have ressources (like an employee) and this ressource can > have certain roles (e.g. a team member, or a team leader). > As you can see in the attachment, I have a Table called > Projects_Ressources. This Table has three relationships (belongs_to). > One to Projects, one to Ressources and one to Roles. > > This is my model description for, > > role.rb: > <code> > class Role < ActiveRecord::Base > has_many :projects_ressources > has_many :ressources, :through => :projects_ressources > has_many :permissions_roles > has_many :permissions, :through => :permissions_roles > end > </code> > > project.rb: > <code> > class Project < ActiveRecord::Base > has_many :projects_ressources > has_many :ressources, :through => :projects_ressources > end > </> > > ressource.rb: > <code> > class Ressource < ActiveRecord::Base > has_many :projects_ressources > has_many :projects, :through => :projects_ressources > has_many :roles, :through => :projects_ressources > end > </code> > > projects_ressources.rb: > <code> > class ProjectsRessource < ActiveRecord::Base > belongs_to :ressource > belongs_to :project > belongs_to :role > end > </code> > > When I''m now in the console, I can get for example the project with id 1 > > >> pro = Project.find(1) > > And can get all the ressources to this certain project: > > >> employee = pro.ressources > > But I want also to know the roles of this ressources. How can I get now > the roles of the ressources to this project with id 1? This doesn''t > work: > > >> role = pro.roles > > produces this error in the console: > > <code> > NoMethodError: undefined method `roles'' for #<Project:0x4dffdc0> > from > c:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record > /attribute_methods.rb:260:in `method_missing'' > from (irb):47 > from :0 > </code> > > Is the design of the relationship wrong or what am I missing? > > Attachments:http://www.ruby-forum.com/attachment/4360/erm.PNG > > -- > Posted viahttp://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.
> But I want also to know the roles of this ressources. How can I get now > the roles of the ressources to this project with id 1? This doesn''t > work: > >>> role = pro.roles > > produces this error in the console: > > NoMethodError: undefined method `roles'' for #<Project:0x4dffdc0> > from > c:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record > /attribute_methods.rb:260:in `method_missing'' > from (irb):47 > from :0 > > Is the design of the relationship wrong or what am I missing?I just started the ruby server again and now it''s working. Maybe a .rb file wasn''t saved or something like that. I get now every role of every ressource that is stored in project_ressources. Thx anyway. -- 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.
Ankur wrote:> Hi ! > > According to your relationships: resource :has_many => roles > but you are using pro.roles. It means project should has many > roles.Its totally wrong accordingly. > > > First fetch all project_resources for a particular project. Eg. > > pr_resources = Project.find(1).project_resources > > pr_resources will be a array of pr_resources > > Now you can fetch any employee(resource) and its role associated with > project id 1. > > for eg. for any project_resource: > > resource = pr_resource.resource > role = pr_resource.role > > > Thanks, > > > -- > ~ Ankur Gupta > Company: www.innozon.com, HyderabadI made already an update of the project.rb, because I found out that the rule was missing. That''s why it worked for ressources but not for projects. Thx a lot for your explanation, I''m still in the learning process of RoR :-) This is now all clear to me. -- 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.