
Problem with belongs_to association model naming
Reported by Dan Higham | February 1st, 2008 @ 05:31 PM
It seems when a model has a belongs_to association to a foreign model where the foreign model ends in an "s" an error is raised when trying to use that association.
For example if you have a model called ticket and that ticket has an association to a model called status through a belongs_to association i.e
Class Ticket < DataMapper::Base
property :title, :string, :nullable => false
property :description, :text
belongs_to :status
End
Class Status
property :name, :string, :nullable => false
End
Calling the status property on the ticket causes the following error;
undefined method `to_sym' for nil:NilClass"
hope that makes sense!
Comments and changes to this ticket
-
asceth February 1st, 2008 @ 09:52 PM
Change your Ticket class to have:
class Ticket < DataMapper::Base property :title, :string, :nullable => false property :description, :text belongs_to :status, :class => Status end
You probably want to put a has_many or has_one in the Status class as well:
class Status property :name, :string, :nullable => false has_many :tickets end
The reason behind this is that in belongs_to the class name is passed through the inflector causing problems so defining it like I listed above fixes the problem. I believe afrench is adding this into the documentation to clarify it for future use.
-
Dan Higham February 2nd, 2008 @ 02:30 PM
Works like a treat! I did add :class => 'Status' but this didn't work, how come it works by passing actual reference to the class itself?
-
asceth February 3rd, 2008 @ 01:26 PM
When you pass a String it still runs through the inflector which assigns the association_constant_name to Statu. Passing the actual reference does not go through the inflector and assigns it properly.
-
Adam French February 9th, 2008 @ 07:17 PM
- State changed from new to open
things working?
can we close this bad boy out?
-
Nick Plante February 15th, 2008 @ 05:23 PM
- State changed from open to resolved
Working as described here. Pls reopen if you feel there is still an issue.
Please Sign in or create a free account to add a new ticket.
With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »