在rails控制台中更新嵌套属性

时间:2014-11-30 13:45:44

标签: ruby-on-rails ruby

如何更新嵌套属性?我收到这个错误:

2.1.3 :021 > user.contact_requests.message = "dsfkjsdklfhdsf"
NoMethodError: undefined method `message=' for #<ActiveRecord::Associations::CollectionProxy []>
    from /Users/bli1/.rvm/gems/ruby-2.1.3/gems/activerecord-4.2.0.beta2/lib/active_record/relation/delegation.rb:136:in `method_missing'
    from /Users/bli1/.rvm/gems/ruby-2.1.3/gems/activerecord-4.2.0.beta2/lib/active_record/relation/delegation.rb:99:in `method_missing'
    from (irb):21
    from /Users/bli1/.rvm/gems/ruby-2.1.3/gems/railties-4.2.0.beta2/lib/rails/commands/console.rb:110:in `start'
    from /Users/bli1/.rvm/gems/ruby-2.1.3/gems/railties-4.2.0.beta2/lib/rails/commands/console.rb:9:in `start'
    from /Users/bli1/.rvm/gems/ruby-2.1.3/gems/railties-4.2.0.beta2/lib/rails/commands/commands_tasks.rb:68:in `console'
    from /Users/bli1/.rvm/gems/ruby-2.1.3/gems/railties-4.2.0.beta2/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
    from /Users/bli1/.rvm/gems/ruby-2.1.3/gems/railties-4.2.0.beta2/lib/rails/commands.rb:17:in `<top (required)>'
    from /Users/bli1/.rvm/gems/ruby-2.1.3/gems/activesupport-4.2.0.beta2/lib/active_support/dependencies.rb:248:in `require'
    from /Users/bli1/.rvm/gems/ruby-2.1.3/gems/activesupport-4.2.0.beta2/lib/active_support/dependencies.rb:248:in `block in require'
    from /Users/bli1/.rvm/gems/ruby-2.1.3/gems/activesupport-4.2.0.beta2/lib/active_support/dependencies.rb:233:in `load_dependency'
    from /Users/bli1/.rvm/gems/ruby-2.1.3/gems/activesupport-4.2.0.beta2/lib/active_support/dependencies.rb:248:in `require'
    from /Users/bli1/Development/RoR/Boothie/bin/rails:8:in `<top (required)>'
    from /Users/bli1/.rvm/gems/ruby-2.1.3/gems/activesupport-4.2.0.beta2/lib/active_support/dependencies.rb:242:in `load'
    from /Users/bli1/.rvm/gems/ruby-2.1.3/gems/activesupport-4.2.0.beta2/lib/active_support/dependencies.rb:242:in `block in load'
    from /Users/bli1/.rvm/gems/ruby-2.1.3/gems/activesupport-4.2.0.beta2/lib/active_support/dependencies.rb:233:in `load_dependency'
    from /Users/bli1/.rvm/gems/ruby-2.1.3/gems/activesupport-4.2.0.beta2/lib/active_support/dependencies.rb:242:in `load'
    from /Users/bli1/.rvm/gems/ruby-2.1.3/gems/spring-1.1.3/lib/spring/commands/rails.rb:6:in `call'
    from /Users/bli1/.rvm/gems/ruby-2.1.3/gems/spring-1.1.3/lib/spring/command_wrapper.rb:38:in `call'
    from /Users/bli1/.rvm/gems/ruby-2.1.3/gems/spring-1.1.3/lib/spring/application.rb:180:in `block in serve'
    from /Users/bli1/.rvm/gems/ruby-2.1.3/gems/spring-1.1.3/lib/spring/application.rb:153:in `fork'
    from /Users/bli1/.rvm/gems/ruby-2.1.3/gems/spring-1.1.3/lib/spring/application.rb:153:in `serve'
    from /Users/bli1/.rvm/gems/ruby-2.1.3/gems/spring-1.1.3/lib/spring/application.rb:128:in `block in run'
    from /Users/bli1/.rvm/gems/ruby-2.1.3/gems/spring-1.1.3/lib/spring/application.rb:122:in `loop'
    from /Users/bli1/.rvm/gems/ruby-2.1.3/gems/spring-1.1.3/lib/spring/application.rb:122:in `run'
    from /Users/bli1/.rvm/gems/ruby-2.1.3/gems/spring-1.1.3/lib/spring/application/boot.rb:18:in `<top (required)>'
    from /Users/bli1/.rvm/rubies/ruby-2.1.3/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from /Users/bli1/.rvm/rubies/ruby-2.1.3/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from -e:1:in `<main>'2.1.3 :022 > user.contact_requests.messages = "dsfkjsdklfhdsf"

这是我的架构:

enter image description here

型号:

class User < ActiveRecord::Base
  # before_save { |user| user.email = user.email.downcase }
  has_many :contact_requests
  # validate the presence of the attributes
  validates(:first_name, presence: true)
  validates(:last_name, presence: true)
  validates(:email, presence: true)
  accepts_nested_attributes_for :contact_requests

end

class ContactRequest < ActiveRecord::Base
  belongs_to :user
  validates :user_id, presence: true
  validates :message, presence: true, length: { maximum: 500 }
end

当我这样做时:

user = User.new
user.email = "email@email.com"
user.last_name = "li"
user.first_name = "brad"
user.phone_number = "21321323"

一切正常,但当我做的时候

user.contact_requetst.message = "jdksfhjskdhfd"

我收到上面显示的错误。

user.save只会保存users部分

1 个答案:

答案 0 :(得分:2)

这是因为user.contact_requests是对象的集合。您不能以这种方式为集合设置属性,而应该使用

选择单个对象
user.contact_requests.first.message = "jdksfhjskdhfd"
例如,

或者,如果要为所有请求设置消息,则应

user.contact_requests.map { |r| r.message = "jdksfhjskdhfd" }