1

Rails 3.1.3 アプリのサインアップ プロセス内でユーザーが状態を持つことができるようにするために、state_machine を採用しようとしています。非常に単純なケースを作成しようとしていますが、イベントによって状態を変更できません。ドキュメントを数回読み直した後、何が問題なのかわかりませんでした。

私の User ActiveRecord モデルは次のとおりです。

# == Schema Information
#
# Table name: users
#
#  id                 :integer         not null, primary key
#  name               :string(255)
#  email              :string(255)
#  created_at         :datetime
#  updated_at         :datetime
#  encrypted_password :string(255)
#  salt               :string(255)
#  admin              :boolean         default(FALSE)
#  notify_followers   :boolean         default(TRUE)
#  state              :string(255)
#

# MME per a utilitzar les Hash functions
require 'digest'

class User < ActiveRecord::Base

  attr_accessor :password # MME nomes dona acces a la instance var @password que no es guarda a la BBDD

  # MME si es posa, atributs (columnes) als que es podrà accedir via ActiveRecord
  attr_accessible   :name, :email, :password, :password_confirmation, :admin, :notify_followers
  # MME validacions
  validates :name, :presence => true,
                   :length=> {maximum: 50}

  validates :email, :presence => true,
                    :format => { :with => /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i },
                    :uniqueness => { :case_sensitive => false}

  validates :password, :presence => true,
                       :confirmation => true,   # crea un atribut password_confirmation i a la vegada confirma que sigui igual que password
                       :length => { :within => 6..40 }

    # validates :password_confirmation, :presence => true   # MME aixo exigigeix que al crear es passi un :password_confirmation, doncs amb nomes
                              #   l'anterior validator sol, pot crearse un usuari si no es passa :password_confirmation

  before_save :encrypt_password

  # MME a l'esborrar un User s'esborren tb els seus Micropost
  has_many :microposts, :dependent => :destroy

 # MME Afegim respostes als usuaris
  has_many :replies, :class_name => 'Micropost',
                     :foreign_key => "in_reply_to",
                     :inverse_of => :replied_user,
                     :dependent => :destroy

  # User com a seguidor (follower)

  # te molts :relationships apuntant-lo amb la clau follower_id. Si el User s'elimina tots aquests Relationship tambe seran eliminats.
  has_many :relationships, :foreign_key => "follower_id",
                           :dependent => :destroy

  # te molts seguits via :relationships als que s'apunta via :followed_id  (inferit gracies a :followed, que apunta a la vegada als User)
  has_many :following, :through => :relationships,
                       :source => :followed

  # User com a seguit (followed)

  # te molts :reverse_relationships apuntant-lo amb la clau followed_id. Si el User s'elimina tots aquests Relationship tambe seran eliminats.
  has_many :reverse_relationships, :class_name => "Relationship",
                                   :foreign_key => "followed_id",
                                   :dependent => :destroy

  # te molts seguidors via :reverse_relationships als que s'apunta via :follower_id  (inferit gracies a :follower, que apunta a la vegada als User)
  has_many :followers, :through => :reverse_relationships

  # Torna els microposts dels usuaris seguits per un user, per exemple:
  #    usr=User.find(12)
  #    usr.following_microposts
  # (no el faig anar finalment: Micropost.from_users_followed_by(user) ho he implementat sense aquests metode perque
  # em falten els microposts del propi user) 
  has_many :following_microposts, :through => :following, 
                                  :source => :microposts

  # Si n'hi ha, te un password_reminder
  has_one :password_reminder

  # Torna l'User de l'email si el password es correcte
  def self.authenticate(email, submited_pwd)
    if usr = find_by_email(email)
      usr.has_password?(submited_pwd) ? usr : nil
    else
      nil
    end
  end

  # Torna l'User del id si el salt es correcte (s'utilitza per les sessions)
  def self.authenticate_with_salt(id, salt)
    user = find_by_id(id)
    (user && user.salt == salt) ? user : nil
  end

  # verifica si el password correspon a l'User
  def has_password?(submited_pwd)
    self.encrypted_password == encrypt(submited_pwd)
  end

  def feed
    #Micropost.from_users_followed_by self
    # Microposts from
    #   self
    #   self.following
    #   self.replies
    Micropost.not_messages.from_users_followed_by_or_in_reply_to self
  end

  # Is usr being followed by self?
  def following? usr
    following.include? usr
    # MME segons el tutorial seria
    #relationships.find_by_followed_id(followed)
  end

  def follow! usr
    relationships.create! :followed_id => usr.id
  end

  def unfollow! usr
    relationships.find_by_followed_id(usr.id).destroy if following?(usr)
  end

  def replies_to(usr, content)
    microposts.create :content=>content, :in_reply_to=>usr.id, :private=>false
  end

  def sends_to(usr, content)
    microposts.create :content=>content, :in_reply_to=>usr.id, :private=>true
  end

  def messages_to usr
    microposts.messages.where(:in_reply_to => usr.id)
  end

  def messages_from usr
    usr.microposts.messages.where(:in_reply_to => self.id)
  end

  def messages_to_or_from usr
    Micropost.messages.between usr, self
  end
  alias conversation_with messages_to_or_from

  # MME generates a unique login name for a user
  def pseudo_login_name
    name.downcase.split.join("_")+"_"+ id.to_s
  end

  # MME generates a password reminder if it doesn't yet exist
  def generate_password_reminder
    #PasswordReminder.find_or_create_by_user_id_and_token :user_id=>self.id,
    #                                                     :token=>SecureRandom.hex(32)
    create_password_reminder!(:token=>SecureRandom.hex(32)) unless password_reminder
  end

  # MME removes its password reminder if exists
  def remove_password_reminder
    password_reminder.delete if password_reminder
  end

  # finds a user from a token (password reminder to change password)
  def self.find_by_token(token)
    pr=PasswordReminder.find_by_token(token, :include=>:user)
    pr.user if pr
  end

  # MME finds a user from a pseudo_login_name
  # first tries to get it from an id
  # last tries to get it from a name
  def self.find_by_pseudo_login_name(pln)
    nam=pln.split("_")
    id = nam.last.to_i
    if id>0 # First attempt: if it exists an id as the last part off the pln 
      User.find_by_id(id)
    else # Second attempt: try to generate a name from a pln
      User.find_by_name(nam.map(&:capitalize).join(" "))
    end
  end

  ## MME state_machine per a fer la inscripcio en passos
  state_machine :initial => :pending do
    event :email_confirm do
      transition :pending => :email_confirmed
    end
  end


  # FUNCIONS PRIVADES
  private

    def encrypt_password
      self.salt = make_salt unless has_password?(password)  # self.salt resets everytime user changes its password
      self.encrypted_password = encrypt(password)   # password refers to self.password
    end

    def make_salt
      Digest::SHA2.hexdigest "#{Time.now.utc}--#{password}"
    end

    def encrypt(str)
      Digest::SHA2.hexdigest "#{salt}--#{str}"
    end

end

もちろん、ユーザーがステートマシンに対応できるようにするための移行はすでに行っています

$ rails g migration AddStateToUser state:string
$ rake db:migrate

そして、ユーザーが Rails コンソールから状態属性に実際に応答することを確認しました。

このコンソール セッション ログのようにマシンの状態を単純に変更しようとすると、問題が発生します。

1.9.2-p290 :006 > u=User.find 1
  User Load (0.5ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 1]]
 => #<User id: 1, name: "Marcel", email: "mme@gmail.com", created_at: "2012-04-29 10:43:42", updated_at: "2012-04-29 10:43:42", encrypted_password: "d08c12c1cfb51fe5732f5e423b94dfdcaca1a1eb67821e3e37a...", salt: "78dfbecdfd4ffdd1fbcac5a878529b91a5200d563ebe3af23cf...", admin: false, notify_followers: true, state: "pendant"> 
1.9.2-p290 :007 > u.state
 => "pendant" 
1.9.2-p290 :008 > u.email_confirm
   (0.5ms)  SELECT 1 FROM "users" WHERE (LOWER("users"."email") = LOWER('mme@gmail.com') AND "users"."id" != 1) LIMIT 1
 => false 
1.9.2-p290 :009 > u.state
 => "pendant" 

お気づきかもしれませんが、最後のコマンドから、私のユーザーは想定どおりに状態を :email_confirmed に変更していません。ところで、実行されている SQL クエリもわかりません。私には疑わしいようです。

それについてもっと。いつものように User モデルを更新しようとすると、同じ奇妙な SQL クエリが表示され、モデルが更新されません。このセッション ログは、次のことを示しています。

1.9.2-p290 :001 > u=User.find 1
  User Load (55.3ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 1]]
 => #<User id: 1, name: "Marcel Massana", email: "xaxaupua@gmail.com", created_at: "2012-04-29 19:32:26", updated_at: "2012-04-29 20:44:10", encrypted_password: "2ef5fec3287e2b26600521488060f698abed387e18e395d1331...", salt: "fa4d3ebb44c00237b66c95cc75ed5d1cda3b6e1535082def2a8...", admin: true, notify_followers: true, state: "pending"> 
1.9.2-p290 :002 > u.update_attributes(:name=>"Marcel")
   (0.1ms)  SAVEPOINT active_record_1
   (0.4ms)  SELECT 1 FROM "users" WHERE (LOWER("users"."email") = LOWER('xaxaupua@gmail.com') AND "users"."id" != 1) LIMIT 1
   (0.1ms)  ROLLBACK TO SAVEPOINT active_record_1
 => false 

誰が何が悪いのか教えてもらえますか? ヒントはありますか?

(もちろん、user.state="email_confirmed" を変更できますが、state_machine を使用する理由は何ですか?)

4

3 に答える 3

1

追加の SQL クエリは、検証の結果です。

validates :email, :uniqueness => { :case_sensitive => false }

DB をチェックして、id != 1その (小文字の) 電子メールを持つ別のユーザー ( ) が既に存在するかどうかを確認します。

于 2012-06-05T22:03:31.987 に答える
0

OK、何が起こっているのかわかったようです:

state_machineたとえば、を変更するたびに、次のようになります。

$ u.email_confirm

state_machine更新される唯一の属性として(my Userインスタンス)をUser#update_attributes内部的に呼び出します。これは、メソッドが呼び出され、その前に検証もチェックされることを意味します。他の属性が更新されない限り、一部の検証では保存が禁止されている可能性があるため、明らかに変更されていません。ustateUser#saveuu.state

それを克服するために、私は単にすべての検証を州内に置きます。総括する:

class User < ActiveRecord::Base
    ...
    state_machine :initial => :pending do

       event :email_confirm do
         transition :pending => :email_confirmed
       end

      state :pending do

        validates :name, :presence => true,
                         :length=> {maximum: 50}

        validates :email, :presence => true,
                          :format => { :with => /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i },
                          :uniqueness => { :case_sensitive => false}

        validates :password, :presence => true,
                             :confirmation => true,
                             :length => { :within => 6..40 }
      end
    end
    ...
  end

検証は常に保存の直前に呼び出されるため、からに移行するときは:pendingすでに値があり、検証は実行されません。:email_confirmedu.state:email_confirmed

さらに、奇妙な(少なくとも私にとっては)SQLクエリ

SELECT 1 FROM "users" WHERE (LOWER("users"."email") = LOWER('xaxaupua@gmail.com') AND "users"."id" != 1) LIMIT 1

検証が有効になっている場合にのみ実行されます。検証を無効にすると、このクエリは実行されません。ActiveRecordがそのクエリを実行する理由がわかりません。これは今のところ私にとって問題ではありませんが、その問題に少し光を当ててくれた人に感謝するか、その行動を説明するリンクを教えてください。

于 2012-05-01T15:16:08.430 に答える