ActiveRecord and use of has_one & has_many?

You need to use belongs_to instead of has_one on the project model You also need to add a project_type_id column on to the projects table.

You need to use belongs_to instead of has_one on the project model. You also need to add a project_type_id column on to the projects table.

Okay so further reading tells me that has_one is strictly for 1:1 relationships and not what I am trying to do, so that makes sense, if a little linguistically weird that now a Project belongs to a ProjectType. Re adding a project_type_id, what if I want the field to be called type not project_type? Is it enough to change t.

Integer type to t. Integer type_id and if so what do I add to my model? I've tried making those changes and now my test throws an error NoMethodError: undefined method 'name' for nil:NilClass – Dave Sag Sep 1 at 0:38 I've also now tried your suggestion of changing the migration to read t.

Integer project_type_id in the projects table but I get exactly the same error. – Dave Sag Sep 1 at 0:42 Did you rollback and rerun the migration now that you've modified it? Did you also specify: belongs_to :project_type on the project model?

As for calling it "type", that's a reserved word used for single table inheritance, so don't do it. – Cameron Walsh Sep 1 at 0:47 Also, in your tests, specify: project = Project. Create(:name => 'Test Project', :project_type => pt) assert project.

Project_type. Name == 'Test Pt', "Wrong Project Type Name, expected 'Test PT' but got '#{project. Project_type.

Name}'. " i.e. Use :project_type instead of :type – Cameron Walsh Sep 1 at 0:49 1 Great, that works.My problem was wanting to use type as a field name and not twigging that it is a reserved word.

(should have been obvious really). Many thanks. – Dave Sag Sep 1 at 1:01.

So a Project has_one :project_type (called type) and a ProjectType has_many :projects. The SQL seems to be assuming that there is a project_id field in the project_types table but that makes no sense if a ProjectType can be associated with many Projects. I suspect that my problem is something to do with my wanting to be able to refer to the ProjectType as project.

Type not project. Project_type, but I am not sure how I'd fix this.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions