gpt4 book ai didi

ruby-on-rails - has_many 关联属性 setter 是否始终将其值保存到数据库中,是否可以不这样做?

转载 作者:行者123 更新时间:2023-12-04 06:17:34 24 4
gpt4 key购买 nike

我有以下脚本向我展示了 has_many roles=属性始终以持久的方式工作。

我的问题是:

1)这种行为背后的原因是什么:为什么 has_many 属性在它们被设置的那一刻就被持久化了?为什么这与常规属性行为(以下脚本中的 name)不同?

2)我可以写我的自定义roles= setter 所以我可以使用 fx assign_attributes对于一堆没有角色关联的模型属性(包括角色=)要持久化?如果在 Rails > 3.2 中可能的话,我会很感激吗?

这是脚本:

gem 'rails', '>=3.2.0' # change as required
gem 'sqlite3'

require 'active_record'
require 'logger'

puts "Active Record #{ActiveRecord::VERSION::STRING}"
ActiveRecord::Base.logger = Logger.new(STDERR)

ActiveRecord::Base.establish_connection(
:adapter => 'sqlite3',
:database => ':memory:'
)

ActiveRecord::Schema.define do
create_table :users, :force => true do |t|
t.string :name
end

create_table :user_roles, :force => true do |t|
t.integer :user_id
t.integer :role_id
end

create_table :roles, :force => true do |t|
t.string :name
end
end

# Create the minimal set of models to reproduce the bug
class User < ActiveRecord::Base
has_many :user_roles
has_many :roles, :through => :user_roles
end

class UserRole < ActiveRecord::Base
belongs_to :user
belongs_to :role
end

class Role < ActiveRecord::Base
end

r = Role.create(:name => 'admin')
u = User.create

# roles= persists its value, name= does not
u.assign_attributes({ :roles => [r], :name => 'Stanislaw' })

# The same behavior is produced by:
# u.attributes=
# u.roles=

puts "name attribute: #{u.name}"
puts "many roles #{u.roles}"

u.reload

puts "name attribute: #{u.name}"
puts "many roles #{u.roles}" # I see admin role and I want to achieve behavior that I would not see it

最佳答案

关联与属性不同。例如,对于 has_many 关联,您在分配时所做的就是在belongs_to 端设置外键。

class User < ActiveRecord::Base
has_many :posts
end

class Post < ActiveRecord::Base
belongs_to :user
end

p = Post.create
u = User.create
u.posts << p # this line will simply update p.user_id with u.id

在您的示例中,将角色分配给用户的连接表将创建一个 UserRole 记录并设置 user_id/role_id 记录。发生这种情况是因为您声明了 has_many :through
至于防止这种行为,您可以使用存储非持久角色的虚拟属性,直到您保存记录,然后创建关联。
class User < ActiveRecord::Base
attr_accessor :unpersisted_roles
attr_accessible :unpersisted_roles

after_save :assign_roles

def assign_roles
self.roles << @unpersisted_roles if defined(@unpersisted_roles)
end
end

r = Role.create
u = User.create
u.attributes = {:unpersisted_roles => [r]}
u.save # roles get persisted here

这只是一个简单的例子,实际代码可能需要更复杂,或者需要更深入地研究 AR 的界面才能让它在没有太多副作用的情况下工作。

如果您可以就为什么不想坚持关联提供一些见解,我可能会建议更具体的行动方案。

更新

引用 Issue #3有一些评论,其中进行了更改。
module SimpleRoles
module Many
module Persistence
class << self
def included base
base.class_eval %{
has_many :user_roles
has_many :roles, :through => :user_roles
# Add a callback to persist the roles
after_create :persist_roles
}
end
end

def roles
# Apply unpersisted roles in case we want to access them before saving
super.map(&:name).map(&:to_sym) + (@unpersisted_roles || [])
end

def roles= *rolez
rolez.to_symbols!.flatten!

# if we're already persisted then go ahead and save
# otherwise stash them in an ivar array
if persisted?
super retrieve_roles(rolez)
else
@unpersisted_roles = rolez
end
end

private

# Our callback method that sets the roles, this will
# work since persisted? is true when this runs.
def persist_roles
self.roles = @unpersisted_roles
end

def retrieve_roles rolez
raise "Not a valid role!" if (rolez - config.valid_roles).size > 0

rolez.map do |rolle|
begin
Role.find_by_name! rolle.to_s
rescue
raise "Couldn't find Role for #{rolle}. Maybe you need to re-run migrations?"
end
end
end

def config
SimpleRoles::Configuration
end
end
end
end

关于ruby-on-rails - has_many 关联属性 setter 是否始终将其值保存到数据库中,是否可以不这样做?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15020016/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com