Reputation: 353
So having some troubles how to resolve my puzzle
.
I'm having 2 models
1) Mode1.rb
class Model1 < ActiveRecord::Base
set_table_name "Model1"
set_sequence_name "Model1"
module Validate
def validate_discount
errors.add(:discount, "#blank") if discount.blank?
end
end
end
2) Model2.rb
class Model2 < ActiveRecord::Base
include Model1::Validate
validate :validate_discount
end
What i need? The trouble is that on submit page operating model2
, so i need to execute validation from there to get proper error display, but as discount
exists only in model1
i need to pass it to model2
The error what i get is now:
undefined local variable or method `discount' for #<Model2:0x12c952f8>
Might i need somehow pass it through the controller? I mean smth like this:
Model2.new
Model2["discount"] = 20
Model2.discount
I'm stuck now.
Upvotes: 0
Views: 103
Reputation: 1920
I think you can use attr_accessor
for this purpose. With that you can set and get value of discount
attribute.
class Model2 < ActiveRecord::Base
include Model1::Validate
attr_accessor :discount
validate :validate_discount
end
With this you can call:
model2 = Model2.new
model2.discount = params[:discount] #or whatever you set value for discount
And then validate it with your Model1::Validate
module.
Upvotes: 1