simple_eav icon indicating copy to clipboard operation
simple_eav copied to clipboard

Does not support rails virtual attributes

Open bnisbett opened this issue 13 years ago • 7 comments

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.

bnisbett avatar Nov 24 '11 20:11 bnisbett

How about a failing test for the expected behavior? I don't have enough context to help.

timlinquist avatar Nov 25 '11 22:11 timlinquist

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 avatar Nov 26 '11 07:11 bnisbett

@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?

timlinquist avatar Dec 03 '11 01:12 timlinquist

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.

timlinquist avatar Dec 03 '11 01:12 timlinquist

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

bnisbett avatar Dec 05 '11 15:12 bnisbett

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.

timlinquist avatar Dec 27 '11 16:12 timlinquist

Yups, I already tried using attr_accessor which is what I was trying to say in my previous comment, I had no luck

bnisbett avatar Jan 03 '12 14:01 bnisbett