Open jjercx opened 3 years ago
yes, your belongs_to
is clashing with Airrecord client
class method. Probably it would be better to raise a warning message or an exception when defining the belongs_to
method here, if an Airrecord method is being overriden:
def has_many(method_name, options)
safely_define_method(method_name.to_sym) do
# Get association ids in reverse order, because Airtable's UI and API
# sort associations in opposite directions. We want to match the UI.
ids = (self[options.fetch(:column)] || []).reverse
table = Kernel.const_get(options.fetch(:class))
return table.find_many(ids) unless options[:single]
(id = ids.first) ? table.find(id) : nil
end
safely_define_method("#{method_name}=".to_sym) do |value|
self[options.fetch(:column)] = Array(value).map(&:id).reverse
end
end
def safely_define_method(method_name, &block)
# Warn if method exists
if method_defined?(method_name)
warn "#{self.name}: overriding method '#{method_name}'!"
end
end
send(:define_method, method_name, &block)
end
Note I haven't tested this implementation. What do you think @sirupsen ?
I was unable to create a record in my
Sale
model. Then I found that it's because I have a relationship namedclient
and I think it's collisioning with Airrecord::Table.client.This is the problem:
But it works if I comment the relationship:
Is this the actual error? What are the alternatives for a fix? (I work on a solution with guidance, would be my first time) Can we document
client
is reserved?