Rails. Add a virtual attribute to a User object - ruby-on-rails

I am developing an API in Rails 3. I am fetching users from the database. This works fine but I want to add an "attribute" to the output called Contact_data. This "attribute" or what it is called does not have a counterpart in the database I just need it in the JSON output so that I can add an array to it.
How can this be done?
The current output is: http://pastie.org/1849048. I would like for it to appear for example just below the bio attribute.

Related

How to create a 'file' model in Rails?

So I have built an API using Ruby/Sinatra and MondoDB. I'm now writing an Admin panel using Rails that connects to the same database. One of our collections is named 'files' since we need to model an object of such kind in our API. Now, when I wanted to create the File model in the Admin Panel, I came across the fact that I can't create a class called 'File'. Doing so results in the error attached
Even 'File1' is an acceptable class name. How do I now create the File model in my Rails app? I'm using Rails 5.0.0.1.
I believe that is happening because File is a reserve Rails word. I would probably rename the model to something like FileResource .
List of Reserved words: http://www.rubymagic.org/posts/ruby-and-rails-reserved-words
The file is ruby's class, you can't and should't use the name in model. Or you will replace the original File .
FileResource is a good name!

saving object to database

I have a ruby on rails 4 app and I'm using omniauth-facebook gem for authentication. I have
#omniauth = request.env["omniauth.auth"]
Now I'd like to save a user's education history (#omniauth[:extra][:raw_info][:education]) in the education column of Auth table in the database. #omniauth[:extra][:raw_info][:education] is an Array object which contains OmniAuth::AuthHash objects (i.e. #omniauth[:extra][:raw_info][:education][0].class returns OmniAuth::AuthHash).
Rails api says that "Active Record can serialize any object in text columns using YAML. To do so, you must specify this with a call to the class method serialize. This makes it possible to store arrays, hashes, and other non-mappable objects without doing any additional work." So I did the following and it seems to be working:
auth.education = #omniauth[:extra][:raw_info][:education]
auth.save
Is there any downside to this? Is there a better way (like using json, etc)? One downside I can think is that saving data this way makes it difficult for search and query which I probably won't be needing.
Thanks a lot.

Building and storing complex SQL query for later editing

I am using Rails 3.2.8 to build a "product set" builder that mirrors Google Analytics' Custom Profile builder. For example, a user may define a product set as follows:
(Category = 'Printers') and ((Name contains 'Wireless') or (Name contains 'Wifi'))
My product data is stored in Postgres (9.1.4) using an HStore column to store the dynamic product attributes. I have built a form that can construct the query using Arel but am stuck on the following requirements:
1. The query must be serialized to the database. I can store the .to_sql string but am then stuck with...
2. I must be able to reconstruct the user's form for later editing, as these are not one-time searches but rather shared queries.
How can I serialize in such a way that I can easily reconstruct the user defined query?
Couldn't you just serialize the parameters that rails gets from the form?
You'd have a hash with the keys and values from the user inputted form, and could easily feed it back into whatever logic you use to query the database from said form, or further process it.
You can have persistent model for saving the user's options about the search.
When you need to run the query, you will get the user's saved options and will pass them to custom method that use ransacker. You can access values in the hstore like this:
Arel::Node::InfixOperator.new('->>', 'hstore_column_name', Arel::Nodes::Quoted.new('key_in_hstore'))

Adding id column and populating it to an existing table in Rails?

I have an existing table that I'd like to use for a Rails application.
It's a simple table with only 4 columns. However it does not yet have id column. And also new data will be added periodically.
I am trying to find a way to add the id column and populate it.
I guess I have two options, but being a noob I am sure there are better ways.
Option 1: I can add the id column and populate it when I parse raw data into CSV files, and then import it to the Rails database. In this case, when I parse the data into CSV files, I need to figure out how to find the last used unique id is.
Option 2: Parse raw data into CSV files, then import to the Rails database. Then my rails application will populate the id column for the new data entries.
If Rails has a built in method or GEM that can populate the id fields for the new entries, that would be great. In that case I will go with the Option 2.
If not, I think it's easier to go with the Option 1.
So I guess the question becomes this: Can Rails automatically populate the id column of entries with blank id field?
Thanks!
Either option should work because the underlying database will automatically handle the id column since it's the primary key. So create the table using a migration, then parse, import, and add the CSV data to your database via which ever method sits best.

custom properties in Rails

I have just started Rails and have a basic question.
I need to add customer properties(like email id etc) so that the Rails app can read them at runtime. How can I do this ?
Can I add them to development.rb and if so how can I read it ?
In java I would have created a properties file and read it from my app.
thank you,
firemonkey
Are you trying to do store and load configuration settings?
It's easy to store configuration settings in a yaml file and load them with initializers - loads better than littering your environment files.
This Railscast: http://railscasts.com/episodes/85-yaml-configuration-file shows you how.
I'm not sure exactly what you are asking. I'm guessing you want an initial set of data in the database that you can access when you actually run the app? If that is so check out this other SO question How (and whether) to populate rails application with initial data
It's a little unclear exactly what you're trying to do, but it sounds like maybe you have a model called Customer and you would like to add some attributes to it, such as email address, id, and so on?
Basically, with Active Record you don't need to do anything special to add a simple attribute (like a string or an integer). Just add a field called "email_address" to your customers table in the database, and all of your Customer objects will automagically get "email_address" and "email_address=" methods (not to mention the Customer class itself getting "find_by_email_address" and other useful methods as well). If you are adding a field containing another model, it's a bit more complicated - add a "something_id" field to the table, and an association to the class definition (eg, "has_one :something"). For more information, see the ActiveRecord api documentation.
You don't have to use any particular means to add the field to your database, but you might want to consider Migrations. Migrations are a convenient way to keep your schema versioned and synchronized across multiple machines.
If you are building your model right now, there's a short cut built in to the generator to add fields. Instead of just saying...
script/generate scaffold customer
...you can say...
script/generate scaffold customer email:string name:string badge_number:integer
...and it will generate all the appropriate fields in your migration, as well as adding them to your generated views.

Resources