How would I access a models attribute?
For example, the User model. Let's say I want to access the email portion of the User object.
It does not seem I am able to do
User.email
To access the email of the user.
I was wondering how I would access object attributes in the model and controller.
To get email for all User records, you can do
User.pluck(:email)
If you want to get email a particular user object, you can do
User.find(1).email
Where 1 being the id of the user object in the users table.
I suggest you to read about ActiveRecord query interface to understand better.
Related
We have a custom field with a type lookup in Zendesk connected to a form. Which gives us the possiblity to add another user object to a ticket.
But I am not able to figure out how to get that email address from that field in the ticket.
The documentation describes that if you select a relation you get a agent or user object. By getting this object, you can address the fields for that object.
But this is not working for us.
For example, the field ID 11963774080017
So to address the name in the field, we use {{ticket.ticket_field_11963774080017}}.
But when using the following {{ticket.ticket_field_11963774080017.user.email}}.
I am not able to fetch the user email.
Many thanks.
In my application I have a user object model. I'm using koala and OmniAuth to access Facebook.
I'm trying to figure out how to add the data from Facebook to my model in such a way that my controller serializes it properly.
I already managed to get the data from Facebook in my controller like that:
token = request.env["omniauth.auth"]["credentials"]["token"]
api = Koala::Facebook::API.new(token)
api.get_object("me")
And I think I figured up how to add an attribute to my Model without a DB column, but in a way that will keep it in the serialized response:
def fb_info
{"fb_info" =>"should be here"}
end
def attributes
super.merge('fb_info' => fb_info)
end
So what is the preferred way to put it all together? I need to somehow populate attributes in my model. In order to do that I need the access token in there. It seems messy to make calls from the controller to populate them, or to pass the token to the model. Is there a clean way to handle this?
Add setter method to your model, and perform all data transformations inside this method. In most cases, you can pass data to setter method from a controller exactly the same way as it can be done with any 'real' attribute. But to store this data into database you should handle it by yourself.
def virtual_column_name=(raw_data)
# some code to handle raw_data and put it into database
end
I am building an app in which the users can have different roles (like seller, buyer, agency, etc). So my plan is use polymorphic association, build a class for each of the roles, and associate each instance with one user account. Each user can only be of one of those types, and after reading on the subject I concluded that this is better than using STI, but correct me if I am wrong.
So the app will have different sign up screens for the main types of user accounts. For instance, in the Seller sign up form, what will happen is that the user will fill in the details required for his user account and the fields specific for the Seller profile.
So this form should create the user object, and then the seller object associated to the former. How do you handle this? My guess is that this form should correspond to the 'new' action of the sellers controller, and in the create action the user account should be created before finally creating the seller.
Is this correct? If so, should I call the User controller create action from the Seller controller, or call directly the User model? If it's the former please provide some example code, because I am not sure about how I should call one controller from another.
EDIT: I also considered using a multipart form, which is probably easier, but before deciding I want to check out this option.
If you're bent on doing it this way, I'd say just call the model from the create method of the Seller controller. What type of relationship do you have between the User model and the Seller model? Because you'd need to do something like this:
def create
user = User.create(params[:user])
seller = Seller.new(params[:seller])
seller.user_id = user.id
seller.save
redirect_to #wherever
end
Here I just assumed you have a belongs_to :user in the Seller model. Still I would advise you to consider a gem like cancan or something to handle roles instead of this approach.
Good luck!
You can use nested form. A user has one role. You can view this railscast: http://railscasts.com/episodes/196-nested-model-form-part-1, it explained how to use nested form. You will be calling User controller when will create User and Role.
I'd take a different approach for this question. Have one roles class. Then create methods in the user class like.
def can_assign_users?
roles.map(&:name).includes('admin')
end
You might have 50 models in a few years otherwise. Plus there a plenty of gems that work like this so you can leverage them.
I want to give users the option of registering as a specified kind of user. The user type field however is in a separate table (for security reasons) so when I create a new account I also create a new record in the other table for that account.
So as far as I can tell, I can either create a new field in my devise created user table and then after_create have my model check for that field and then add it to my associated user permissions table. But that seems redundant, so is there a way to add some sort of temporary variable to the form and use that to create the user type?
When you want to create/edit two or more objects in the same form then you should user nested forms. You will find all necessery information in this two railscasts: http://railscasts.com/episodes/196-nested-model-form-part-1 and http://railscasts.com/episodes/197-nested-model-form-part-2
Typically, with Authlogic, you would authenticate with some unique identifier (e.g. email or username) and a password. Authlogic does this by first finding the record by the identifier (User.find_by_email_address(email) or similar), then calling then .authenticate method on the returned object, which checks the password against the record.
What I want to do is change the way Authlogic finds the record (at a finer level than just changing the field it uses). My use case requires that the email_address column (in this case) is in an associated child model.
Essentially, I want to ask Authlogic to find my User record based on the presence of an associated Subscriber model that is uniquely identified by the email_address that is provided.
Has anyone seen Authlogic accommodate this?
You might probably want to configure a different #login_field.