如何插入到轨道中的多个表 [英] how to insert into multiple tables in rails

查看:150
本文介绍了如何插入到轨道中的多个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图插入使用Rails多个表.. 我有一个表称为用户和服务。 当我创建一个用户的用户信息应该进入的用户和服务名和用户ID应该进入服务表。 任何帮助将大大appriciated。

i am trying to insert into multiple tables using rails.. i have a table called users and services. when i create a user the user details should go into users and a service name and userid should go into services table. Any help would be greatly appriciated.

感谢。

推荐答案

您可以添加一个回调来创建一个新的服务创建用户时。

You can add a callback to create a new service when a user is created.

class User < ActiveRecord::Base
    def after_create
        Service.create!(:name => 'my_service_name', :user_id => self.id)
    end
end

该after_create法新用户创建后触发。因此,一个新的服务将被创建每次创建新的用户的时间。

The after_create method is triggered after a new user creation. So a new service will be created every time you create a new user.

您可以找到有关在 Rails的指南

这篇关于如何插入到轨道中的多个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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