4

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

我的问题是:

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

2)我可以编写我的自定义roles=设置器,以便我可以将 fxassign_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
4

1 回答 1

3

关联与属性不同。例如,对于 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 的界面才能使其正常工作而不会产生太多副作用。

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



更新

参考问题 #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
于 2013-02-22T08:52:34.107 回答