I have two classes, ProjectTypes
, and RiskTypes
, both are many to many, so both have a has_and_belongs_to relationship on the model.
When I run rails c
I can do the following:
r1 = RiskType.last
which returns the last risk type
#<RiskType:0x00007f15e0a20d58
id: 5,
Rtype_name: "test type",
and the same for project type
p2 = ProjectType.last
and the relationship works as when I run
r1.project_types
I get the project type I set in my seed, but now if I want to change that so that r1 is now related to p2 I get an error.
r1.project_types = p2
the error is:
/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/activemodel-6.1.4.1/lib/active_model/attribute_methods.rb:469:in `method_missing': undefined method `each' for #<ProjectType id: 2, Ptype_name: "EDD", created_at: "2022-09-27 08:17:58.382009000 0000", updated_at: "2022-09-27 08:17:58.382009000 0000"> (NoMethodError)
This is obviously some basic understanding of relationships I don't understand yet, any help much appreciated...
CodePudding user response:
Since project_types is an array, you have to assign it as follows:
r1.project_types = [p2]