#6 楼 @meeasyhappy 这个可能不能作为正式环境的方案。另,我的代码流程很简单,不会有你说的这类操作的。
ruby
DEBUG - (0.1ms) BEGIN
DEBUG - Account Exists (0.3ms) SELECT 1 AS one FROM `accounts` WHERE LOWER(`accounts`.`email`) = LOWER('[email protected]') LIMIT 1
DEBUG - SQL (0.2ms) INSERT INTO `accounts` (`blogs_count`, `comments_count`, `created_at`, `crypted_password`, `email`, `logo`, `name`, `profile_image_url`, `profile_url`, `provider`, `role`, `uid`) VALUES (0, 0, '2015-05-04 21:52:14', '$2a$10$7FoH64NjksN39nKI7gm7zu/36dlbgxWYpdn5590JWxjhlJersyN6W', '[email protected]', NULL, 'aijinsong', NULL, NULL, NULL, 'admin', NULL)
DEBUG - (0.5ms) COMMIT
#1 楼 @lgn21st 非常感谢。已解决,详细描述如下。在这里抛出另外一个问题:
DEBUG - Account Exists (1.0ms) SELECT 1 AS one FROM `accounts` WHERE LOWER(`accounts`.`email`) = LOWER('[email protected]') LIMIT 1
如上是一条 DEBUG 信息。第一次学习使用 ruby 和 ActiveRecord,提示信息给到的反馈,让我误以为是数据已存在。所以希望能够弄清楚 ActiveRecord 给到这个 DEBUG 信息是何解?
以下为根据提示调整后的日志和代码。
DEBUG - RELOAD (0.3626s) /Users/aijinsong/Documents/projects/opensource/robbin_site/app/controllers/room.rb
DEBUG - (0.9ms) BEGIN
DEBUG - Account Exists (1.0ms) SELECT 1 AS one FROM `accounts` WHERE LOWER(`accounts`.`email`) = LOWER('[email protected]') LIMIT 1
DEBUG - (0.5ms) ROLLBACK
{:role=>["是无效的"]}
修改了代码,增加了对 role 的赋值就对了。
@accountTemp = Hash.new
@accountTemp['name'] = 'aijinsong'
@accountTemp['email'] = '[email protected]'
@accountTemp['password'] = 'aiajsajs'
@accountTemp['password_confirmation'] = 'aiajsajs'
@accountTemp['role'] = 'admin'
temp = Account.create(@accountTemp)
#4 楼 @ChanceDoor 后续我会找时间继续和您探讨这个问题。
#2 楼 @aijinsong 正确的写法如下。希望对大家有帮助。
class Party < ActiveRecord::Base
belongs_to :party , :polymorphic => true
has_many :parties_telecom_numbers
end
class Person < ActiveRecord::Base
has_one :party, :as => :party, :dependent => :destroy
has_many :parties_telecom_numbers, :as => :party
has_many :telecom_numbers, :through => :parties_telecom_numbers
end
class Customer < ActiveRecord::Base
has_one :party, :as => :party
has_many :parties_telecom_numbers, :as => :party
has_many :telecom_numbers, :through => :parties_telecom_numbers
end
class TelecomNumber < ActiveRecord::Base
end
class PartiesTelecomNumber < ActiveRecord::Base
belongs_to :party , :polymorphic => true
belongs_to :telecom_number
end
#1 楼 @ChanceDoor 非常感谢您的提示。稍后我会将可用方案给出。