Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
Improve this question
I have an api definition in json and I want to convert that to an old ruby hash representation like :
{:hello => 'there', ....}
Actually I want to use that with a create() statement really to add objects to db based on that definition.
I do want to use => because I am very used to it and frankly like it much more than the :
Is there also a tool that can convert that to a migration too? (I can alter the datatypes when needed).
I have quite a few of these APIs and it will take a lot of time to do that manually so I am wondering whether there's some sort of quick to use converter for such a case.
Thanks!
You can simply use
Model.create(JSON.parse(json_string))
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I need algorithm that assign object to cluster already created.
In my case objects are Patients or sick person and clusters are diseases, and based on multiple questions patient should be assigned to cluster; of course the questions and responses (yes or no) will be already prepared for every disease.
For example : you are coughing ? if yes => do you have a fever? if Yes then maybe => corona disease.
and so on.
edit : decision tree algorithm
Have a look at ID-3; this allows you to build a decision tree based on pre-classified data, and any new patient can be classified according to their responses.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
I have 300 questions and answers and each of the questions fall in one of four categories. My question is what would be the best way to create web searchable pages for each 300 Q&A.
My first thought is to just do it simple and create one model. The one model will have question:string answer:text category:string and then input all the questions and answers in the database.
My second choice would be to create a category model and then a model for questions and answers.
My third choice is to create a JSON file with the questions and answers formatted then call on it through Javascript.
What would be the best way to achieve this while allowing search engines to rank each question?
I would say, go with single model Faq which contains all the required info i.e. question, answer, and category.
You don't need a separate model for categories if:
The categories list is predefined (in which case you can use enum type field), AND
Each category has only a name and no other attributes.
About JSON file, it will be difficult to add/remove FAQs in that. You will probably need to push your code after every change. With a model Faq in application, you can always do CRUD on FAQs easily.
So, the final structure should be like this:
class Faq
# field :question
# field :answer
# field :category, type: :enum, values: %w[<category-names>]
end
And for rendering FAQs category-wise, you can always group on category.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Consider Item model that may have zero or more variations, how can I found first item which have some variations?
class Item
has_many :variations
end
So item.variations is not nil.
Something like:
Item.with_not_nil_variations.first
If your item has many variations, what you want is a SQL INNER JOIN.
In rails you can do it using joins :
Item.joins(:variations).first
The generated SQL will contain an INNER JOIN, meaning that it will return the Items having a variation. Before the first you can add on order(), that will allow you to have more control on the first items with variations that will be returned.
Do not use includes as it is translated to a left outer join, and it is definitely not what you want.
An inner join should do this for you:
Item.joins(:variations).first
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Say I have some object array:
employees:[
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Peter", "lastName":"Jones"}
]
Can I use Ruby's include? method to do something like this:
employees.include?("'firstName' = 'John'")
Use Enumerable#any? instead:
employees.any?{ |e| e["firstName"] == "John" }
Enumerable is an included module of Array so that should work with arrays as well.
This assumes JSON code has been parsed from raw JSON string to Ruby arrays and hashes.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
In Rails, how can you allow your users to have capital letters in their usernames (like GreatestOneEver) but still have case insensitivity when searching (like User.find_by_username "greatestoneever") or performing other tasks? Basically, I want my users to see that their username looks capitalized, but then behind the scenes, use a lower cased version in all code.
I have one idea: add a new field to the model, called display_name or something. Is there a better way?
Edit:
I don't want to use a search by regex or something because that's inefficient. I really need to index by username! But then have case-insensitive lookups too. Maybe display_name is the best idea?
Here is one possibility
name = "GreatestOneEver"
User.find(:first, :conditions => [ "lower(name) = ?", name.downcase ])
However I'm guessing if you're searching case insensitive then you may get back multiple results. So you might remove the :first.
try humanize it may works if your string are with underscore(_) OR For search process try, following regex:
{username: /#{Regexp.escape(search)}/i}
Where search is your value.