在Rails 4中属于:conditions [英] belongs_to with :conditions in Rails 4

查看:145
本文介绍了在Rails 4中属于:conditions的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个模型,用户和帐户:

I have two models, Users and Accounts:

class User < ActiveRecord::Base
  belongs_to :account, :conditions=>proc{" company_account = #{self.company_user} "}
end

class Account < ActiveRecord::Base
  has_many :users
end

在Rails 3中 belongs_to :conditions => 可以正常工作,但是在Rails 4中,我读到此选项无效。

In Rails 3 belongs_to and :conditions=> works fine, but in Rails 4 I read this options is not valid.

我尝试了 belongs_to:account,-> {其中company_account:self.company_user} ,但出现错误未定义方法 company_user

I tried belongs_to :account, -> {where company_account: self.company_user} but I get error undefined methodcompany_user

如何在Rails 4中解决这个问题?

How can I solve this in Rails 4?

推荐答案

我通过在帐户模型中使用组合主键解决了我的问题。

I solved my problem, by using a composed primary key in my Account model.

为此,我使用了此宝石

这篇关于在Rails 4中属于:conditions的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆