This doesn’t work because company is set on the Entry in the create action,
like this:
def create
… @entry.company = current_user.company
Why have you got Entry belongs_to company? If it also belongs_to user
and user belongs_to company then you can just say @entry.user.company.
If you want to be able to say @entry.company then you can use
delegate.
The :assigned_user relationship should only be to a user who is related
to
the same company as :user. I validate it like this:
def assigned_user_must_be_in_company
if assigned_user.present? && assigned_user.company_id !=
user.company_id
errors.add(:assigned_user, “anvndaren mste tillhra samma fretag”)
end
end
Which works OK.
But it would be nice to also have a constraint for this on the relation
itself to get the correct users automatically etc.
Den mndagen den 18:e februari 2013 kl. 11:00:21 UTC+1 skrev Colin L.:
Also, about the relationship with company. If Entry is not related to
Company, how do you deal with:
A user is deleted (the company may still want to see her Entries?)
Don’t actually delete the user while she still has entries. Just mark
her as inactive or whatever is appropriate. If the entries are still
of interest then it may be of interest who created the entry even if
she has gone.
Isn’t it inefficient to get all Entries from a company?
company.users.each do …
Instead of just:
company.entries
Company
has_many :users
has_many :entries, :through => :users