【发布时间】:2014-03-26 21:30:47
【问题描述】:
我是 Rails 新手,请见谅。 我有一个现有的数据库架构
create_table "registrations", force: true do |t|
t.string "name", null: false
t.string "product1", null: false
t.string "product_serial1", null: false
t.string "product2", null: false
t.string "product_serial2", null: false
t.string "product3", null: false
t.string "product_serial3", null: false
t.string "product4", null: false
t.string "product_serial4", null: false
end
在 UI 上,我希望能够搜索序列号,它需要搜索 product1、product2、product3...
我的想法是让模型联合起来,这样所有的产品都会在自己的行中返回,就像
select name, product1 product, serial1 serial from registrations where product1 != ''
union all
select name, product2 product, serial2 serial from registrations where product2 != ''
union all
select name, product3 product, serial3 serial from registrations where product3 != ''
union all
select name, product4 product, serial4 serial from registrations where product4 != ''
Rails 可以在模型中做这样的事情吗?就像控制器一样,如果我注册.search(),它会默认返回上面的 SQL。
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-4 model