多对多联接表中的额外属性

时间:2022-10-05 11:48:16

I have a many to many association between User and Todo through a join model called UserTodo

我通过名为UserTodo的连接模型在User和Todo之间建立了多对多的关联

Of the many users a todo has, there's one owner. So, I created a column in the user_todos join table called is_owner.

在todo拥有的众多用户中,有一个所有者。因此,我在user_todos连接表中创建了一个名为is_owner的列。

Question is, how do I populate this attribute while creating a Todo?

问题是,如何在创建Todo时填充此属性?

Currently, I'm creating the todo, then separately updating this attribute in TodoController#create action.

目前,我正在创建待办事项,然后在TodoController #create action中单独更新此属性。

@todo = current_user.todos.create(todo_params)
@todo.user_todos.first.update_attribute(:is_owner, true)

This seems wrong. Is there a single call I can make to populate this attribute while creating the todo?

这似乎是错的。在创建待办事项时,是否可以进行一次调用来填充此属性?

Second, is there a way to query if a user is an owner of a todo, this way?

第二,有没有办法通过这种方式查询用户是否是待办事项的所有者?

current_user.todos.first.is_owner?

3 个解决方案

#1


1  

I would make a user_todo.rb file with a UserTodo class and do stuff like:

我会使用UserTodo类创建一个user_todo.rb文件并执行以下操作:

ut=UserTodo.new
ut.todo = Todo.create(todo_params)
ut.user = current_user
ut.is_owner = true
ut.save

current_user.todos_as_usertodos.first.is_owner?

current_user.todos_as_usertodos.first.is_owner?

You can make on user.rb

你可以在user.rb上制作

def todos_as_usertodos
  UserTodo.where(user_id: id).to_a
end

See where I'm going with this? You want to return and use UserTodo objects vs. Todo objects because they have more info in them. The info you need. That extra is_owner boolean. When you goto just a plain todo object you lose that info.

看看我要去哪里?您想要返回并使用UserTodo对象与Todo对象,因为它们中包含更多信息。您需要的信息。额外的is_owner布尔值。当您转到一个普通的todo对象时,您会丢失该信息。

#2


0  

That seems a bad way since you use .first to get your instance. I'll do something like

这似乎是一种糟糕的方式,因为你使用.first来获取你的实例。我会做点什么的

UserTodo.create(user: current_user, is_owner: true, todo: Todo.create(todo_params))

Second

第二

I'm not sure if that is possible

我不确定这是否可行

#3


0  

You can use this one-liner to create the join model with extra attributes:

您可以使用此单行创建具有额外属性的连接模型:

current_user.user_todos.create(todo: Todo.create(todo_params), is_owner: true)

Since is_owner is an attribute of the join model, you have to access it through that model too:

由于is_owner是连接模型的属性,因此您还必须通过该模型访问它:

current_user.user_todos.first.is_owner?

#1


1  

I would make a user_todo.rb file with a UserTodo class and do stuff like:

我会使用UserTodo类创建一个user_todo.rb文件并执行以下操作:

ut=UserTodo.new
ut.todo = Todo.create(todo_params)
ut.user = current_user
ut.is_owner = true
ut.save

current_user.todos_as_usertodos.first.is_owner?

current_user.todos_as_usertodos.first.is_owner?

You can make on user.rb

你可以在user.rb上制作

def todos_as_usertodos
  UserTodo.where(user_id: id).to_a
end

See where I'm going with this? You want to return and use UserTodo objects vs. Todo objects because they have more info in them. The info you need. That extra is_owner boolean. When you goto just a plain todo object you lose that info.

看看我要去哪里?您想要返回并使用UserTodo对象与Todo对象,因为它们中包含更多信息。您需要的信息。额外的is_owner布尔值。当您转到一个普通的todo对象时,您会丢失该信息。

#2


0  

That seems a bad way since you use .first to get your instance. I'll do something like

这似乎是一种糟糕的方式,因为你使用.first来获取你的实例。我会做点什么的

UserTodo.create(user: current_user, is_owner: true, todo: Todo.create(todo_params))

Second

第二

I'm not sure if that is possible

我不确定这是否可行

#3


0  

You can use this one-liner to create the join model with extra attributes:

您可以使用此单行创建具有额外属性的连接模型:

current_user.user_todos.create(todo: Todo.create(todo_params), is_owner: true)

Since is_owner is an attribute of the join model, you have to access it through that model too:

由于is_owner是连接模型的属性,因此您还必须通过该模型访问它:

current_user.user_todos.first.is_owner?