On 18 February 2013 09:44, Linus Pettersson <linus.pettersson@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
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
No comments:
Post a Comment