Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getter for property #303

Closed
quolpr opened this issue Oct 26, 2015 · 1 comment
Closed

Getter for property #303

quolpr opened this issue Oct 26, 2015 · 1 comment

Comments

@quolpr
Copy link

quolpr commented Oct 26, 2015

Hello! I didn't find any information about getter for property, that will be populated based on another field, but not through html form. For example, I have models:

# == Schema Information
#
# Table name: users
#
#  id           :integer          not null, primary key
#  phone        :string
#

class User < ActiveRecord::Base
end

and

# == Schema Information
#
# Table name: orders
#
#  id          :integer          not null, primary key
#  user_id     :integer
#

class Order < ActiveRecord::Base
  belongs_to :user
end

So i want to set User to the Order model. I can get User model only based on phone field from html form. I tried to do like here:

class OrderForm < Reform::Form
  property :user_id, getter: ->(params) { User.find_by(phone: PhonyRails.normalize_number(phone)).id }
end

But it doesn't work. Also I tried to do it through populator - nothing is happend too.
I found solution, but I think it is not so good:

def user_id
  @fields['user_id'] ||= User.find_by(phone: PhonyRails.normalize_number(phone)).id
end
@quolpr
Copy link
Author

quolpr commented Oct 26, 2015

Found solution in #277

@quolpr quolpr closed this as completed Oct 26, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant