使用带有Rails 3.2的.build方法时,如何设置ActiveRecord模型属性(多对多)

时间:2022-10-04 15:47:07

I'm working on a sort of project management app with Rails (my Rails skills is kinda rusty). I have two model objects, in this case User and Account, which have a many-to-many relationship (Company could maybe be a better name for Account). When a user signs up a new Account is created (with .build) with help form a nested form. The Account model have two fields name and account_admin. When the the initial user creates it's Account I want to set account_admin to the users id. But I can't get this to work.

我正在使用Rails开发一种项目管理应用程序(我的Rails技能有点生疏)。我有两个模型对象,在这种情况下是用户和帐户,它们具有多对多关系(公司可能是帐户的更好名称)。当用户注册时,创建一个新帐户(使用.build)并在帮助下创建一个嵌套表单。帐户模型有两个字段名称和account_admin。当初始用户创建帐户时,我想将account_admin设置为用户ID。但我不能让这个工作。

The models is set up like this:

模型设置如下:

class Account < ActiveRecord::Base
  attr_accessible :name, :account_admin

  validates_presence_of :name 

  has_many :projects, dependent: :destroy
  has_many :collaborators
  has_many :users, through: :collaborators
end 

class User < ActiveRecord::Base
  has_secure_password
  attr_accessible :email, :name, :password, :password_confirmation, :accounts_attributes

  has_many :collaborators
  has_many :accounts, through: :collaborators
  accepts_nested_attributes_for :accounts
  [...]

The UserController looks like this:

UserController看起来像这样:

def new
  if signed_in?
    redirect_to root_path
  else
    @user = User.new
    # Here I'm currently trying to set the account_admin value, but it seems to be nil. 
    account = @user.accounts.build(:account_admin => @user.id)
  end
end  

I have also tried to move account = @user.accounts.build(:account_admin => @user.id) to the create action, but the the field disappears from the form.

我还尝试将account = @ user.accounts.build(:account_admin => @ user.id)移动到create操作,但该字段从表单中消失。

What would be the appropriate way to accomplish what I want (set account_admin to the users id when it is getting created)? Or is there a better approach to find out which user created the account (ie. do something with the relationship table)?

什么是完成我想要的适当方法(在创建时将account_admin设置为用户ID)?或者是否有更好的方法来找出哪个用户创建了帐户(即用关系表做某事)?

Update

更新

With help from @joelparkerhenderson I think I got it to work. I made a method in my User model that looks like this:

在@joelparkerhenderson的帮助下,我想我得到了它的工作。我在我的User模型中创建了一个如下所示的方法:

def set_account_admin
  account = self.accounts.last
  if account.account_admin == nil
    account.account_admin = self.id
    account.save
  end
end

Which I call with after_create :set_account_admin. This works, but is there a more "Rails way" to do the same?

我用after_create调用它:set_account_admin。这是有效的,但有更多的“Rails方式”来做同样的事情吗?

Thanks.

谢谢。

1 个解决方案

#1


1  

When you call #new, the user doesn't have an id yet (it is nil).

当你调用#new时,用户还没有id(它是nil)。

When you #save the user, Rails automatically gives the user a new id.

当你#save用户时,Rails会自动为用户提供一个新的id。

You can then use the after_create Active Record callback to set the new Account's account_admin

然后,您可以使用after_create Active Record回调来设置新帐户的account_admin

#1


1  

When you call #new, the user doesn't have an id yet (it is nil).

当你调用#new时,用户还没有id(它是nil)。

When you #save the user, Rails automatically gives the user a new id.

当你#save用户时,Rails会自动为用户提供一个新的id。

You can then use the after_create Active Record callback to set the new Account's account_admin

然后,您可以使用after_create Active Record回调来设置新帐户的account_admin