simple_eav
simple_eav copied to clipboard
Does not support rails virtual attributes
Came across another issue where simple_eav removes the attributes that make use of the validates_acceptance_of validation in active_model. Since the attribute is only virtual it treats it as a custom attribute and the validation therefore does not take place. I am not sure how to approach this.
How about a failing test for the expected behavior? I don't have enough context to help.
Here is what i'm getting at, in the example below, the person should not be allowed to subscribe unless they have accepted the terms of service. with terms of service set to 0 the object should not be valid. However simple_eav removes that terms of service key-value before it goes to active record and treats it like a custom attribute because "terms_of_service" does not have a column in the db.
class Person < ActiveRecord::Base
include SimpleEav
configure_simple_eav :simple_attributes
validates_acceptance_of :terms_of_service
end
it "should know virtual attributes of an object and allow them to be validated" do
params = {"name"=>"John Doe", "age"=>"25","email"=>"[email protected]","terms_of_service"=>"0"}
subscriber = User.new(params)
subscriber.should_not be_valid
end
@bnisbett Are virtual attributes still supported in Rails ? It seems to me adding a getter and/or setter for :terms_of_service should resolve the issue?
I guess they are. This should help out: http://railscasts.com/episodes/167-more-on-virtual-attributes. I'm pretty familiar with this part of Active Record. If the setter is defined it'll use it when iterating over the params.
Let me know if this is still an issue after updating your class per the link above.
yes I had already tried using a setter, which is actually not supposed to be necessary because the validates_acceptance_of does all of this for you already. The issue remains the same, validating the acceptance of this attribute does not work once simple_eav is included in the class.
The exact feature I am referring to is described here in the rails guides: http://guides.rubyonrails.org/active_record_validations_callbacks.html#acceptance
Right I think they use method_missing to do this like simple_eav does. You need to define a getter/setter on the class for the "virtual attribute" ie : attr_accessor :your_attribute.
Let me know if that doesn't work.
Yups, I already tried using attr_accessor which is what I was trying to say in my previous comment, I had no luck