For a blog model I'm saving an RSS field as text under Blog.rss, problem is, some of this is rather long and each one prints when I'm working in the rails console, ie: Blog.last(10).
Is there a way to hide output unless I call someblog.rss specifically?
I had a similar problem and received some solutions in another forum, which were:
Use select to get just the columns you need
If you have a very long column (I had JSON data structure from a webhook cluttering the console), consider whether you really need it, and if you don't , don't store it in the table
Or, consider storing it in an associated table
if you need the whole object but just want to change how it's represented in console/log output, you can redefine inspect
yourobject.as_json(except: :unwanted_column)
Also
You could look into: https://github.com/awesome-print/awesome_print
I have a datetime value which comes from the API in this format: 2015-07-07T17:30:00+00:00. I simply want to split it up between the date and time values at this point. I am not using an Active Record model and I prefer not to use an sql database if I can.
The way I have set up the app means that the value is "stored" like this in my view: #search.dining_date_and_time
I have tried two approaches to solving this problem:
Manually based on this previous stackoverflow question from 2012: Using multiple input fields for one attribute - but the error I get is the attribute is "nil" even though I put a "try"
Using this gem, https://github.com/ccallebs/split_date_time which is a bit more recent and seems to be a more elegant solution, but after closely following the doc, I get this error, saying my Search model is not initalized and there is no method: undefined method dining_date' for #<Search not initialized>
This is when instead I put #search.dining_date in the view, which seems to be the equivalent of the doc's example (its not that clear). The doc also says the method will be automatically generated.
Do I need to alter my model so I receive the data from the API in another way? ie. not get the variable back as #search.dining_date_and_time from the Search model for any of this to work?
Do I need an Active Record model so that before_filter or before_save logic works - so i can (re)concatenate after splitting so the data is sent back to the API in a format it understands. Can I avoid this - it seems a bit of overkill to restructure the whole app and put in a full database just so I can split and join date/time as needed.
Happy to provide further details, code snippets if required.
As I am not using a conventional Rails DB like MySql Lite or Postgresql, I found that the best solution to the problem was by using this jQuery date Format plugin: https://github.com/phstc/jquery-dateFormat to split the date and time values for display when I get the data back from the API.
The Github docs were not too expansive, but once I put the simply put the library file in my Rails javascript assets folder, I just had to write a few lines of jQuery to get the result and format I wanted:
$(function() {
var rawDateTime = $('#searchDiningDateTime').html();
// console.log(rawDateTime);
var cleanDate = $.format.date(rawDateTime, "ddd, dd/MM/yyyy");
// console.log(cleanDate);
$('#searchDiningDateTime').html(cleanDate);
var cleanTime = $.format.date(rawDateTime, "HH:mm");
// console.log(cleanTime);
$('#searchTime').html(cleanTime);
});
Next challenge: rejoin the values on submit, so the API can read the data by sending/receiving a valid request/response. (The values can't be split like this when sent to the remote service).
Newbie here, wrapping my head 'round this stuff!
I'd like to use the hex number as my url (external identifier) and keep the uuid within the database for a ruby on rails application. Is this even possible?
Thanks a bunch
Many people advise you against it but, yes, it is possible. It will need some code for it, and the solution depends on which version of Rails you use and what you use for the database, which is why I'm going to answer in a generic way.
You will want to have two different fields for the model: one for the external hex representation and another one for a separate UUID. Then, you can use the hex string to find instances in your controller actions, for example.
Please take a look at the following (they don't seem to have the two fields but will point you to the right direction anyway):
Problems setting a custom primary key in a Rails 4 migration
Change Primary Key Issue Rails 4.0
http://www.speakingcode.com/2013/12/07/gracefully-using-custom-primary-keys-in-rails-4-routes-controllers-models-associations-and-migrations/
And a longer post of a similar thing to do: http://ruby-journal.com/how-to-override-default-primary-key-id-in-rails/
Also, the FriendlyId gem might do what you want.
I've got a JSON hash of hashes returned by a website API that I want to parse and display based on a specific key's value within the internal hashes.
I can think of solutions that would achieve this, but they would take a number of lines of code and don't seem efficient. Surely there must be a way to natively in Rails, given the focus on convention over configuration. I've googled around a bit, but found nothing that covers this issue.
Sample Response from API:
[{"banner":"01197271","birthday":"1991-01-11","committee_id":1,"created_at":"2012-08-08T01:56:02-05:00","email":"me#example.com","first_name":"Dan","graduation_date":"May 2013","hometown":"San Antonio","hours_enrolled":15,"id":2,"image":{"url":null,"thumb":{"url":null},"large":{"url":null}},"invitation_accepted_at":null,"invitation_limit":null,"invitation_sent_at":null,"invitation_token":null,"invited_by_id":null,"invited_by_type":null,"last_name":"Tester","local_apt":"","local_city":"San Antonio","local_state":"Texas","local_street":"One UTSA Circle","local_zip":"78249","major":"Computer Science","permanent_apt":"","permanent_city":"","permanent_state":"","permanent_street":"One UTSA Circle","permanent_zip":"","phone":"5558813284","same_address":true,"tour_trained":false,"updated_at":"2012-08-17T03:35:26-05:00","utsa_id":"uoi431"},
{"banner":"","birthday":"1990-10-25","committee_id":null,"created_at":"2012-08-03T16:19:23-05:00","email":"you#example.com","first_name":"Test","graduation_date":null,"hometown":null,"hours_enrolled":null,"id":1,"image":{"url":null,"thumb":{"url":null},"large":{"url":null}},"invitation_accepted_at":null,"invitation_limit":null,"invitation_sent_at":null,"invitation_token":null,"invited_by_id":null,"invited_by_type":null,"last_name":"User","local_apt":"","local_city":"","local_state":"","local_street":"","local_zip":"","major":null,"permanent_apt":"","permanent_city":"","permanent_state":"","permanent_street":"","permanent_zip":"","phone":"","same_address":false,"tour_trained":false,"updated_at":"2012-08-15T10:05:54-05:00","utsa_id":""}]
Potential solution would be to go through each internal hash, determining value of relevant key value, then store, based on where the key value places it compared to already tested hashes. When complete, return.
Ok so if you have objects that are set up to parse this information, those objects can build themselves based off the parameters of your hash. So you could do something like this
object = MyObject.create(your_hash_parameters)
Where your_hash_parameters are the parameters that you presented in your example.
I'm not sure what would happen if there were more paramaters than your object knew what to do with, if it would still build itself or not. If that is the case, you could use the delete_if method to exclude attributes that are unwanted.
One more note, if this isn't something that you want saved to your database, and its only to display temporary information. I would set up a model with attr_accessors that represent the attributes that you are displaying.
As told in comment, I'd create an ActiveResource object and add relevant methods to it.
I am trying to build a ROR app that allows users to enter date in various formats such as 12/31/11 (month/day/year) or 31/12/11 (day/month/year). In order to interpret date format, I will have a select field from where user can select the format of date. I can use Date.strptime(value, format).to_s() in controller before saving record.
However, I am not sure is controller right place to put this information. Is there a way I can push this to model..say in before save method.
You could just save the data as it is (in the controller), and have another field in the model telling it how to interpret the data. Then, in a before_validation callback, you could try parsing the date according to the given format and writing it to the same field, now as a date. Problems may arise on the way back. Then, to display it in the view again, you could write helpers - but better yet, provide a method in the model (for instance, formatted_date) that will compute and display the date.
However, this requirement sounds strange. Why is the user's responsibility to select a date format? Shouldn't it be based on the user locale?
In any case, I suggest you register your date formats in an initializer, rather than repeting the format strings throughout the application.