第一次更新失败,第二次成功

时间:2015-10-30 10:44:36

标签: ruby-on-rails

我们有这个对象,@ current_employer,这表现得有些奇怪。第一次更新失败,第二次更新。

<RelativeLayout
                    android:id="@+id/rlCountry"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="20dp"
                    android:layout_marginRight="20dp"
                    android:layout_marginTop="10dp"
                    android:background="@android:color/white" >

                    <Spinner
                        android:id="@+id/spnCountry"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_centerHorizontal="true"
                        android:layout_centerVertical="true"
                        android:popupBackground="@android:color/transparent"
                        android:visibility="invisible" />

                    <Button
                        android:id="@+id/btnAllergies"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_alignParentLeft="true"
                        android:layout_alignParentStart="true"
                        android:layout_centerInParent="true"
                        android:background="@android:color/transparent"
                        android:gravity="start|center"
                        android:paddingBottom="5dp"
                        android:paddingEnd="5dp"
                        android:paddingLeft="10dp"
                        android:paddingRight="5dp"
                        android:paddingStart="10dp"
                        android:paddingTop="5dp"
                        android:tag="0"
                        android:text="Select Country"
                        android:textColor="#ACACAC"
                        android:textSize="14sp" />
                </RelativeLayout>

我们在这里初始化它:

(byebug) @current_employer.update(settings_params)
false
(byebug) @current_employer.update(settings_params)
true

它只是一个标准&#34;发现&#34;。

目前的解决方法:

  @current_employer = Employer.find(decoded_auth_token[:employer_id])

之前有人见过这个吗?

更新

在&#34; before_save&#34;中跟踪它到这一行。调用

if @current_employer.update(settings_params) || @current_employer.update(settings_params)

...

似乎 # self.is_test = false if is_test.nil? 是保留关键字?

2 个答案:

答案 0 :(得分:0)

<强>解决

完整回调,修复内联注释:

def set_default_values
  self.has_accepted_terms = false if has_accepted_terms.nil?
  self.live = true if live.nil?
  self.account_name.downcase!
  self.display_name ||= account_name
  self.display_name ||= ""
  self.contact_first_name ||= ""
  self.contact_last_name ||= ""
  self.phone_number ||= ""
  self.is_test_account = false if is_test_account.nil?
  true # FIX. The proceeding line was returning 'false', which was giving us a 'false' return value for the before_save callback, preventing the save.
end

答案 1 :(得分:0)

<强>模型

如果它在一个实例中失败并且几乎立即成功,那么典型的问题是您将不正确/冲突的属性传递给模型。

我推测你发送的settings_params有一个阻止保存发生的值。您通过更新提到了这一点:

# self.is_test = false if is_test.nil?

解决这个问题的方法是从你的params散列中删除任何潜在的错误属性:

def settings_params
   params.require(:employer).permit(:totally, :safe, :attributes)
end 

您的模型一致地更新 - 无论出现什么条件。如果它失败了,这意味着模型保存流程中会出现另一个问题。

-

没有看到额外的信息,我无法看到它们可能是什么

<强>更新

设置默认值的更好方法如下:

How can I set default values in ActiveRecord?

您可能希望使用attribute-defaults gem

class Foo < ActiveRecord::Base
  attr_default :age, 18
  attr_default :last_seen do
    Time.now
  end
end
相关问题