Rails的3.2.6。加入2表,并选择记录(表2),以形成(表1)与下拉选 [英] Rails 3.2.6. Join 2 tables and select record(table2) to form(table1) with drop-down-select

查看:101
本文介绍了Rails的3.2.6。加入2表,并选择记录(表2),以形成(表1)与下拉选的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想加入2表,我有一个表人和一张桌子工厂。 我的问题是,我如何才能增加一个人,不是选择(有下拉)工厂的人是属于?

i would like to join 2 tables i have a table 'persons' and a table 'factories'. My question is, how can i add a person and than choose (with a dropdown) the factory the person belongs to?

我的人模型:

class Person < ActiveRecord::Base
  has_many :factories

  has_attached_file :cv

  attr_accessible :cv, :email, :function, :mobile, :telephone
end

我的工厂模式:

My factories model:

class Factory < ActiveRecord::Base
    belongs_to :persons

  attr_accessible :last_name, :email, :telephone, :first_name, :website, :country
end

我想这样做,例如:

I want to do it for example:

|添加个人|

功能:(输入字段)

电话:(输入字段)

手机:(输入字段)

电子邮件:(输入字段)

Email: (input field)

厂(下拉)LT; =这是实际的领域我想有,它是默认情况下不放置在人员表

Factory (Drop-down) <= this is the actual field i would like to have and it is not placed in the persons table by default.

CV:(上传域)

提交:(提交按钮)

我希望你明白我想有,我希望有人可以帮助。

I hope you understand what i would like to have and i hope someone could help.

感谢。

修改1 我改变了我的计划。我想有一个选择的factory.id在人员表中的列。 我在Person表做了一个列名为factory.id现在我想以从该列中的其他表存储数据。 这可能吗?

EDIT 1 i changed my plans. I would like to have a chosen factory.id in a column in the persons table. I made a column in my Person table with the name factory.id now i would to to store the data from the other table in that column. Is this possible?

推荐答案

您也许可以做到这一点:

You can probably do this:

控制器:

@people=Person.new

@factories = Factory.all

查看:

    form_for @people do |a|
.
.
.
    = a.select :factory, options_for_select(@factories.map{ |f| [f.name, f.id] })

这篇关于Rails的3.2.6。加入2表,并选择记录(表2),以形成(表1)与下拉选的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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