两个表服务在Rails的一个模型 [英] Two Tables Serving as one Model in Rails

查看:182
本文介绍了两个表服务在Rails的一个模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

时,可以在导轨安装在模型依赖于两个表中的联接?这将意味着,对于被发现的模型记录/更新/破坏那里将需要连接在一起的联接两个数据库表中均有记载。该模型也只是两个表一起包裹然后可用于形成等的所有列。被创建的模型时,这种方式/更新它是被应用到模型只是一种形式的变量哈希?

Is is possible in rails to setup on model which is dependant on a join from two tables? This would mean that for the the model record to be found/updated/destroyed there would need to be both records in both database tables linked together in a join. The model would just be all the columns of both tables wrapped together which may then be used for the forms and so on. This way when the model gets created/updated it is just one form variable hash that gets applied to the model?

这是有可能在轨道2或3?

Is this possible in Rails 2 or 3?

推荐答案

这是不可能做你的Rails的要求到底是什么的,据我所知,但可以有效地完成你想要什么完成了第二个模型,利用回调和HAS_ONE协会,如:

It isn't possible to do exactly what you're asking for in Rails as far as I know, but you can effectively accomplish what you're trying to accomplish with a second model, using callbacks and a has_one association, for instance:

class Widget < ActiveRecord::Base
  has_one :thingy
  before_save :save_thingy_object

  def save_thingy_object
    self.thingy = Thingy.new({ :attr1 => 'some', :attr2 => 'thing' })
  end
end

class Thingy < ActiveRecord::Base
  belongs_to :widget
end

这篇关于两个表服务在Rails的一个模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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