如何将关联表与 has_one 关联连接起来 [英] how to join an associated table with a has_one association

查看:55
本文介绍了如何将关联表与 has_one 关联连接起来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 Rails 应用中,我只要求用户在注册时输入电子邮件和姓名,然后让他们选择为他们的个人资料提供更完整的联系方式.因此,我有一个与 Contact.rb 关联的 User.rb 模型,即

In my Rails app, I only require users to enter email and name upon signup, but then give them the option to provide fuller contact details for their profile. Therefore, I have a User.rb model that has an association with Contact.rb, namely,

用户.rb

 has_one :contact

联系.rb

 belongs_to :user

Contact.rb 具有您可能期望的可预测字段,例如地址、邮政编码等,但它还存储了与 Province.rb 模型建立关系的 Province_id,因此

Contact.rb has the predictable fields you might expect such as address, postal code etc, but it also stores the province_id for a relation with the Province.rb model, so

联系.rb

 attr_accessible :address, :city, :mobile,  :postalcode, :province_id, :user_id
 belongs_to :user
 belongs_to :province

省.rb

has_many :contacts

我是这样做的(而不是在 contact.rb 上将省的名称存储为字符串"),以便我可以更轻松地(所以我认为)按省对用户进行分类.

I did it that way (rather than storing the name of the province as a "string" on contact.rb) so that I could more easily (so I thought) categorize users by province.

在其中一个 Artist_controller 的 show 动作中,我执行以下操作以检查用户是否尝试按省排序,然后调用进行搜索的 Artist_by_province 方法

In the show action of one of the artists_controller, I do the following to check whether the user is trying to sort by province and then call an artists_by_province method that does a search

    if params[:province_id]
     province = params[:province_id]
     province = province.to_i #convert string to integer

     @artistsbyprovince = User.artists_by_province(province)

     else

     @artists = User.where(:sculptor => true)

     end

这是 User.rb 模型上的方法,如果传入省 ID,它会调用此方法

This is the method on the User.rb model that it calls if a province id is passed in

scope :artists_by_province, lambda {|province|
  joins(:contact).
  where( contact: {province_id: province},
         users: {sculptor: true})

  }

但是它给了我这个错误:

However it gives me this error:

Could not find table 'contact'

如果我将联系人设为复数

If I make contacts plural

scope :artists_by_province, lambda {|province|
  joins(:contacts).
  where( contacts: {province_id: province},
         users: {sculptor: true})

  }

这个错误

Association named 'contacts' was not found; perhaps you misspelled it?

谁能告诉我在进行此查询时我做错了什么?

Can anyone tell me what I'm doing wrong when I'm making this query?

更新:我在发布后更改了一些细节,因为我的复制和粘贴有一些问题

Update: I changed some of the details after posting because my copy and paste had some problems with it

附言忽略我正在寻找雕塑家"这一事实.我更改了问题的用户类型名称.

P.S. ignore the fact that I'm searching for a 'sculptor.' I changed the names of the user types for the question.

来自 schema.rb

from schema.rb

create_table "contacts", :force => true do |t|
    t.string   "firm"
    t.string   "address"
    t.string   "city"
    t.string   "postalcode"
    t.string   "mobile"
    t.string   "office"
    t.integer  "user_id"
    t.datetime "created_at",  :null => false
    t.datetime "updated_at",  :null => false
    t.integer  "province_id"
  end

推荐答案

该问题已通过在 join 中使用 contact(单数)和 contacts(复数)中的where 子句.我猜'contact'(单数)反映了User.rb 和Contact.rb 之间的has_one 关联,而'contacts' 在where 子句中用于表示表的名称,它总是复数.

The problem was fixed by using contact (singular) in the join and contacts (plural) in the where clause. I'm guessing 'contact' (singular) reflects the has_one association between User.rb and Contact.rb, whereas 'contacts' is used in the where clause to represent the name of the table, which is always plural.

用户.rb

  has_one :contact

  scope :artists_by_province, lambda {|province|
  joins(:contact).
  where( contacts: {province_id: province},
         users: {sculptor: true})

  }

这篇关于如何将关联表与 has_one 关联连接起来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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