belongs_to condition that checks the current user?

42 views
Skip to first unread message

Linus Pettersson

unread,
Feb 18, 2013, 4:44:51 AM2/18/13
to rubyonra...@googlegroups.com
Hi

I have a model, let's call it entry, that belongs to a user. The user and the entry also belongs to a company.

Can I add a condition on the belongs_to relations to check this?

Something like:
belongs_to :assigned_user, class_name: "User" , conditions: { company_id: company_id }

This doesn't work because company is set on the Entry in the create action, like this:
def create
...
  @entry.company = current_user.company
....

And current_user is the Devise helper and not available in the model.

So, can I achieve this somehow?

Colin Law

unread,
Feb 18, 2013, 5:00:21 AM2/18/13
to rubyonra...@googlegroups.com
On 18 February 2013 09:44, Linus Pettersson <linus.pe...@gmail.com> wrote:
> Hi
>
> I have a model, let's call it entry, that belongs to a user. The user and
> the entry also belongs to a company.
>
> Can I add a condition on the belongs_to relations to check this?
>
> Something like:
> belongs_to :assigned_user, class_name: "User" , conditions: { company_id:
> company_id }
>
> 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.

Colin

Linus Pettersson

unread,
Feb 18, 2013, 5:24:04 AM2/18/13
to rubyonra...@googlegroups.com, cla...@googlemail.com
That is true. I'll probably change this.

This doesn't solve my problem though. I realize that I phrased myself a bit odd in my first post I will clarify below.

I have three models, User, Entry, Company

A user belongs to a company

An Entry belongs to the User who creates it.

An entry can also be assigned to another user (think of it like a task). So, I have these relationships in Entry:

  belongs_to :user
  belongs_to :assigned_user, class_name: "User"

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, "användaren måste tillhöra samma företag")
    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.

Linus Pettersson

unread,
Feb 18, 2013, 5:37:04 AM2/18/13
to rubyonra...@googlegroups.com, cla...@googlemail.com
Also, about the relationship with company. If Entry is not related to Company, how do you deal with:

1. A user is deleted (the company may still want to see her Entries?)

2. Isn't it inefficient to get all Entries from a company?
company.users.each do ....

Instead of just:
company.entries

Or am I wrong? :)

Colin Law

unread,
Feb 18, 2013, 8:18:34 AM2/18/13
to Linus Pettersson, rubyonra...@googlegroups.com
On 18 February 2013 10:37, Linus Pettersson <linus.pe...@gmail.com> wrote:
> Also, about the relationship with company. If Entry is not related to
> Company, how do you deal with:
>
> 1. 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.

>
> 2. 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

Then you can say @company.entries

Colin
Reply all
Reply to author
Forward
0 new messages