> While you're at it, can we make sure that all DataObject requests for
objects that don't exist return empty objects. You can't chain much of
anything at the moment, because you have to keep testing for an
object.
Hamish, I think changing get_one() will do more harm than good.
I agree that it gets inconsistent when get_one() is used through
a has_one relationship, which returns false instead of an empty
object.
has_many and many_many return an empty and chainable ComponentSet.
Perhaps we need a get_one_or_empty() which you could call explicitly?
And special has_one getters like ParentOrEmpty() instead of Parent()?
Looks ugly, but would be a solution that doesn't break backwards
compatibility.
On Mar 29, 6:54 pm, Sam Minnee <
sam.min...@gmail.com> wrote:
> I don't understand how this would be anything more than renaming
> has_one to belongs_to. They're functionally equivalent, and so it
> seems like a needless change.
No they're not.
class Student {
$has_one = array('Profile'=>'Profile');
}
class Profile {
$has_one = array('Student'=>'Student');
}
Will create both StudentID and ProfileID foreign keys. I'd like to be
able
to create a has_one with just one foreign key, the other side of the
relationship
should work by looking up this foreign key on the other table.
belongs_to would be a solution for this. Sure, I could mimick this
behaviour
by setting one side to a has_many which happens to contain a single
item,
but that will mess up scaffolding interfaces, return a collection
rather
than a single item, and generally look weird.
From the rails documentation
http://apidock.com/rails/ActiveRecord/Associations/ClassMethods/belongs_to.
Also
http://guides.rubyonrails.org/association_basics.html,
particularly "2.7 Choosing Between belongs_to and has_one"
>
> It seems like what we really need is a way of saying which of the
> has_one relationships the given has_many is supposed to connect to.
Yeah, thats an issue that won't be solved by belongs_to.
Sam, I think your example with SiteTree having relations to itself is
more confusing
than helpful for the discussion ;) Here's something a bit easier:
class SiteTree {
static $has_one = array(
'Author'=>'Member',
'Publisher'=>'Member'
);
}
class Member {
static $has_many = array(
'PublishedPages' => 'SiteTree'
'AuthoredPages' => 'SiteTree'
);
}
How do you know which pages belong to Author or Publisher?
This is becoming a bit wider discussion than I anticipated,
from what I understand we're talking about these mostly separate
problems:
1. has_one should have a belongs_to counterpart for "two sided
has_ones"
2. We don't have a way to identify which has_one a has_many belongs
to.
3. How can we "auto-detect" a has_one for a has_many?
4. DataObject::get_one() returns are inconsistent when used in
relations