我有以下脚本向我证明has_many roles=
属性始终以持久的方式工作。
我的问题是:
1)这种行为背后的原因是什么:为什么has_many属性在它们被设置的那一刻就被保留了?为什么这与常规属性行为的区别(以下脚本中的name
)?
2)我可以编写我的自定义roles=
setter,这样我就可以使用fx assign_attributes
来保存一组模型属性(包括roles =)而不保留角色关联吗?如果有可能在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
答案 0 :(得分:3)
关联与属性不同。例如,当你分配的是在belongs_to端设置外键时,你所做的就是has_many关联。
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