我在Rails中对父级父级的嵌套属性进行作用域唯一性验证时遇到问题.
背景
我有一个带有3个型号的rails 4应用程序:
#app/models/account.rb
class Account < ActiveRecord::Base
has_many :contacts, dependent: :destroy
end
#app/models/contact.rb
class Contact < ActiveRecord::Base
belongs_to :account
has_many :email_addresses, dependent: :destroy, validate: :true, inverse_of: :contact
accepts_nested_attributes_for :email_addresses,allow_destroy: true
validates :email_addresses, presence: true
end
#app/models/email_address.rb
class EmailAddress < ActiveRecord::Base
belongs_to :contact, inverse_of: :email_addresses
validates :label, presence: true
validates :contact, presence: true
validates :email, uniqueness: true, presence: true
validates_email_format_of :email
end
Run Code Online (Sandbox Code Playgroud)
问题
我想创建一个范围,以确保模型EmailAddress 的属性:电子邮件在帐户级别是唯一的(帐户是Contact的父级,它本身是EmailAddress的父级).
正如http://guides.rubyonrails.org/active_record_validations.html所建议的,我尝试过:
class EmailAddress < ActiveRecord::Base …Run Code Online (Sandbox Code Playgroud) scope ruby-on-rails validates-uniqueness-of nested-attributes
只是一个我无法在stackoverflow上找到答案的快速问题.
如果很容易为登台和生产提供环境变量(例如在heroku上),我该如何为我的localhost(开发环境)设置环境变量?(我在Mac上)
截至今天,我对开发环境的api凭证进行了硬编码,对此并不满意.
谢谢 !