模型中的before_create方法不起作用 - Rails4

时间:2016-06-09 11:11:20

标签: ruby-on-rails ruby-on-rails-4

我不确定为什么在创建公司时我的布尔值不会自动设置为false。有人可以告诉我为什么。

  • 我正在使用模型中的before_create方法

你的帮助或建议将不胜感激

模式

ActiveRecord::Schema.define(version: ############) do
  create_table "companies", force: true do |t|
    t.string   "companyname"
    t.string   "tel"
    t.string   "email"
    t.boolean  "fake"
  end
end

company.rb

我是否正确编写了set_company_as_false方法?

class Company < ActiveRecord::Base
  belongs_to :category_businesstype
  has_many :users, dependent: :destroy

  before_create :set_company_as_false

  def set_company_as_false
    self.fake == false
    # if false it means company is not a fake/dummy company
  end
end

终端

Started POST "/companies" for 127.0.0.1 at 2016-06-09 12:08:11 +0100
Processing by CompaniesController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"siKCe0npwA/sICpaCSds+vxWLw1ftv7z4s3tuNITJOM=", "company"=>{"companyname"=>"Event Ninja", "companyimage"=>#<ActionDispatch::Http::UploadedFile:0x007f83a463c2c8 @tempfile=#<Tempfile:/var/folders/72/z21dh3tx6jb03dscv7m3wc_h0000gn/T/RackMultipart20160609-772-nbq6po>, @original_filename="eventnj.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"company[companyimage]\"; filename=\"eventnj.png\"\r\nContent-Type: image/png\r\n">, "email"=>"info@eventninja.io", "link"=>"https://twitter.com/eventninjaio", "tel"=>"+447961262477", "category_country_id"=>"3", "city"=>"London", "category_staff_id"=>"1", "category_companyage_id"=>"2", "category_businesstype_id"=>"1", "category_advert_id"=>"25"}, "commit"=>"Register"}
   (0.1ms)  begin transaction
   (0.1ms)  rollback transaction
  Rendered shared/_header_signedout.html.erb (1.8ms)
  CategoryCountry Load (0.3ms)  SELECT "category_countries".* FROM "category_countries"
  CategoryStaff Load (0.2ms)  SELECT "category_staffs".* FROM "category_staffs"
  CategoryCompanyage Load (0.2ms)  SELECT "category_companyages".* FROM "category_companyages"
  CategoryBusinesstype Load (0.2ms)  SELECT "category_businesstypes".* FROM "category_businesstypes"
  CategoryAdvert Load (0.3ms)  SELECT "category_adverts".* FROM "category_adverts"
  Rendered companies/_form.html.erb (956.0ms)
   (0.1ms)  SELECT COUNT(*) FROM "stories"
  Rendered shared/_footer.html.erb (2.0ms)
  CACHE (0.0ms)  SELECT "category_countries".* FROM "category_countries"
  CACHE (0.0ms)  SELECT "category_staffs".* FROM "category_staffs"
  CACHE (0.0ms)  SELECT "category_companyages".* FROM "category_companyages"
  CACHE (0.0ms)  SELECT "category_businesstypes".* FROM "category_businesstypes"
  CACHE (0.0ms)  SELECT "category_adverts".* FROM "category_adverts"
  Rendered companies/_form.html.erb (605.7ms)
  CACHE (0.0ms)  SELECT COUNT(*) FROM "stories"
  Rendered shared/_footer.html.erb (2.4ms)
  Rendered shared/_responsive_companies_form.html.erb (611.9ms)
  Rendered companies/new.html.erb within layouts/application (1579.4ms)
Completed 200 OK in 10049ms (Views: 10041.4ms | ActiveRecord: 1.5ms)

控制器

class CompaniesController < ApplicationController
  respond_to :html, :xml, :json
  before_action :set_company, only: [:show, :edit, :update, :destroy]
  #load_and_authorize_resource

  def new
    if user_signed_in?
      if current_user.admin_pa_management_group
        @user = current_user
        @company = @user.company
      else
        redirect_to errorpermission_path
      end
    else
      @company = Company.new
    end
  end

  def create
    @company = Company.new(company_params)
    respond_to do |format|
      if @company.save
        format.html { redirect_to(new_user_registration_path, notice: 'Company was successfully created.') }
        format.json  { render json: @company, status: :created, location: @company }
      else
        format.html { render action: "new" }
        format.json  { render json: @company.errors, status: :unprocessable_entity }
      end
    end
  end

  private
    def set_company
      @company = Company.find(params[:id])
    end

    def company_params
      params.require(:company).permit(:companyname, :tel, :email, :category_staff_id, :category_country_id, :category_advert_id, :category_businesstype_id, :category_companyage_id, :city, :town, :latitude, :longitude, :address, :image, :link, :companyimage, :postcode, :about, :linklinkedin, :fake)
    end
end

3 个答案:

答案 0 :(得分:1)

请更改您的set_company_as_false方法。

<强> company.rb

class Company < ActiveRecord::Base
  belongs_to :category_businesstype
  has_many :users, dependent: :destroy

  before_create :set_company_as_false

  def set_company_as_false
    self.fake = false
    # if false it means company is not a fake/dummy company
  end
end

希望这对你有用。

答案 1 :(得分:0)

您应该在回调方法上使用assign =运算符。

self.fake = false

此外,我想知道你为什么不在迁移文件中将fake列作为default: false。它比回调更容易和简单。

答案 2 :(得分:0)

正如您的方法所说的set_company_as_false,所以您似乎希望将公司设置为false。因此,方法将是:

def set_company_as_false
   self.fake = false
end

如果您正在尝试做其他事情,请告诉我