如何使用嵌套方法检索刚刚创建的帐户 ID?

发布于 2024-10-22 18:25:07 字数 1041 浏览 1 评论 0原文

我正在使用 Ruby on Rails 3,并且成功使用嵌套模型来保存模型\对象关联。

在用户模型文件中,我有:

class User < ActiveRecord::Base
  has_one :account

  accepts_nested_attributes_for :account
  validates_associated :account
end

@user.save 之后 我想检索刚刚创建的帐户 ID 并将该值保存在用户数据库表中。我需要它,因为我将使用 account_id 作为用户类的外键,但我不知道这是否可能。如果是这样,我该怎么做?


在我的用户模型中我也尝试了以下操作:

before_create :initialize_user

def initialize_user
  user_account = Account.create
  self.account_id = user_account.id
end

但它不起作用。


更新

我尝试了此操作

class User < ActiveRecord::Base
  belongs_to :account,
    :class_name  => "Account",
    :foreign_key => "users_account_id"
end

class Account < ActiveRecord::Base
  has_one :user,
    :class_name  => "User",
    :foreign_key => "users_account_id"
end

,它保存了新帐户。无论如何,在用户数据库表中,users_account_id 列为null,因此foreign_key 值不会自动保存。

I am using Ruby on Rails 3 and I successfully use nested models in order to save model\object associations.

In the user model file I have:

class User < ActiveRecord::Base
  has_one :account

  accepts_nested_attributes_for :account
  validates_associated :account
end

After @user.save I would like to retrieve the account id just created and save that value in the user database table. I need that because I will use the account_id as the foreign key for the user class, but I don't know if it is possible. If so, how can I do that?


In my user model I also tryed the following:

before_create :initialize_user

def initialize_user
  user_account = Account.create
  self.account_id = user_account.id
end

but it doesn't work.


UPDATE

I tryed this

class User < ActiveRecord::Base
  belongs_to :account,
    :class_name  => "Account",
    :foreign_key => "users_account_id"
end

class Account < ActiveRecord::Base
  has_one :user,
    :class_name  => "User",
    :foreign_key => "users_account_id"
end

and it save the new account. Anyway in the user database table the column users_account_id is null and so the foreign_key value isn't saved automatically.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

慈悲佛祖 2024-10-29 18:25:08

方法是错误的。当您有“has_one”关系时,它们的外键位于关联的模型中。所以在你的情况下它会被考虑在内。如果它接受帐户的嵌套属性。如果你正在写的话,默认情况下应该注意这一点。

看看 http://railscasts.com/episodes/196-nested- model-form-part-1 和其他部分,看看嵌套表单是如何工作的

The Approach is wrong. When you have a "has_one" relationship, they foreign key is in the associated model. So in your case it will in account. And if its accepting nested attributes for account. That should be taken care of by default if you are doing it write.

Take a look http://railscasts.com/episodes/196-nested-model-form-part-1 and the other part as well, to see how nested forms work

删除→记忆 2024-10-29 18:25:08
def initialize_user
  user_account = Account.create
  self.account_id = user_account.id
end

应该是,

def initialize_user
  self.account.create
end

当创建新的 Account 实例时,它将自动使用有关当前用户的信息。您的方法会起作用,但您需要添加额外的“保存”调用。

def initialize_user
  user_account = Account.create
  self.account_id = user_account.id
end

should be

def initialize_user
  self.account.create
end

When the new Account instance is being created, it will use the information about the current user automatically. Your method would have worked, but you'd have needed to add an extra "save" call.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文