Где-то в моей модели:
has_many :custom_values, -> { where(custom_field: CustomField.where(field_format: "company"))}, :foreign_key => "value", :primary_key => :id_s, :dependent => :destroy
def id_s
read_attribute(:id).to_s
end
custom_value.value
- это varchar, поэтому я пытаюсь создать строку из id
с помощью метода id_s
Но это не такне работает, оно всегда сравнивает custom_value.value
с NULL
FE:
2.5.0 :037 > WkAccount.first().custom_values
WkAccount Load (0.7ms) SELECT `wk_accounts`.* FROM `wk_accounts` ORDER BY `wk_accounts`.`id` ASC LIMIT 1
CustomValue Load (36.5ms) SELECT `custom_values`.* FROM `custom_values` WHERE `custom_values`.`value` = NULL AND `custom_values`.`custom_field_id` IN (SELECT `custom_fields`.`id` FROM `custom_fields` WHERE `custom_fields`.`field_format` = 'company')
в то время как:
2.5.0 :039 > WkAccount.first().id_s
WkAccount Load (1.2ms) SELECT `wk_accounts`.* FROM `wk_accounts` ORDER BY `wk_accounts`.`id` ASC LIMIT 1
=> "7"
Я не могу понять, почему
WHERE `custom_values`.`value` = NULL
Вместо
WHERE `custom_values`.`value` = "5"
Есть идеи?
РЕДАКТИРОВАТЬ: добавлена вся модель
class WkAccount < ActiveRecord::Base
unloadable
belongs_to :address, :class_name => 'WkAddress', :dependent => :destroy
has_many :billable_projects, as: :parent, class_name: "WkAccountProject", :dependent => :destroy
has_many :invoices, as: :parent, class_name: "WkInvoice", :dependent => :restrict_with_error
has_many :invoice_items, through: :invoices
has_many :custom_values, -> { where(custom_field: CustomField.where(field_format: "company"))}, :foreign_key => "value", :primary_key => "id_s", :dependent => :destroy
has_many :projects, through: :billable_projects
has_many :contracts, as: :parent, class_name: "WkContract", :dependent => :destroy
has_many :opportunities, as: :parent, class_name: "WkOpportunity", :dependent => :destroy
has_many :activities, as: :parent, class_name: 'WkCrmActivity', :dependent => :destroy
has_many :contacts, foreign_key: "account_id", class_name: "WkCrmContact", :dependent => :destroy
has_many :payments, as: :parent, class_name: "WkPayment"
belongs_to :location, :class_name => 'WkLocation'
validates_presence_of :name
validate :hasAnyValues
def id_s
read_attribute(:id).to_s
end
def hasAnyValues
name.blank? && address_id.blank? && activity_id.blank? && industry.blank? && annual_revenue.blank? && assigned_user_id.blank? && id.blank?
end
# Returns account's contracts for the given project
# or nil if the account do not have contract
def contract(project)
contract = nil
unless project.blank?
contract = contracts.where(:project_id => project.id).first
contract = contracts[0] if contract.blank?
end
contract
end
end