Rails passing more than one parameters - sql

Respected ppl ...
I have a grave design issue since many days ...kindly help out ...
Im coming to this stage from the previous screen after entering the hospital_id which u can c from the URL is 10 (using ransack) ... at this stage when i click "Add performance detail for employee" im passing only the employee_id to the performance creation screen ...
How do i capture the hospital_id from the previous screen and store in my performance creation screen ? ... (it has field hospital_id) ....
can i somehow use this raw sql query to store hospital_id directly ? ...
SELECT hospital_id
FROM employee_sanction_working
WHERE employee_id
Pls do help ...
Thnx

There are few ways you can resolve your problem:
1) you can pass hospital_id in parameter as Rubyman and Santhosh K have suggested Or
2) you can create a class variable in controller which you can use in your erb or haml view.
for example:
in controller:
def new
#hospital_id = params[:hospital_id] #this you can use in your view
end
in view
Hospital ID is <%= #hospital_id %>
Please refer this for more information

You can pass the hospital_id as parameter to "Add performance detail for employee" link.

Related

Showing data from multiple tables in Rails

I'm working on a system that tracks illnesses and symptoms associated with them through Ruby on Rails.
I have a "illnesses" table and a "symptoms" table and they have a many_to_many relationship through the "symptoms_illnesses" table.
For now, I'm working on a page that displays a single entry of the "symptoms_illnesses" table. This table has two columns: illness_id and symptom_id.
I need a way to display the illness and symptom that match the ID.
Example:
"illness" table has "Common Cold" under id = 1
"symptom" table has "Fever" under id = 1
"symptoms_illness" table has illness_id = 1 and symptom_id = 1.
I want the "symptoms_illness/1" page to display "Common Cold" and "Fever", but I see no obvious way to do it.
Other topics in this site don't seem to address the problem in a satisfactory way.
Edit 1:
I didn't add anything to the show action besides the "default"
def show
#symptoms_illness = Symptoms_illness.find(params[:id])
end
def show
#symptoms_illness = Symptoms_illness.includes(:illness,:symptom).where(id: params[:id])
end
in your view
<%= #symptoms_illness.illness.name %>
<%= #symptoms_illness.symptom.name %>
PS: not tested
After doing some research I found out how to do it:
First, I created #illness and #symptom instances to the symptom_illness_controller. Then, in the show action, I added two find_by_id.
Finally, in the Show view, I changed #symptomillness.illness_id to #symptomillness.illness.name, now it shows up fine.
This may not be the cleanest way to do it but it works for what I wanted to accomplish.

Rails Order by frequency of a column in another table

I have a table KmRelationship which associates Keywords and Movies
In keyword index I would like to list all keywords that appear most frequently in the KmRelationships table and only take(20)
.order doesn't seem to work no matter how I use it and where I put it and same for sort_by
It sounds relatively straight forward but i just can't seem to get it to work
Any ideas?
Assuming your KmRelationship table has keyword_id:
top_keywords = KmRelationship.select('keyword_id, count(keyword_id) as frequency').
order('frequency desc').
group('keyword_id').
take(20)
This may not look right in your console output, but that's because rails doesn't build out an object attribute for the calculated frequency column.
You can see the results like this:
top_keywords.each {|k| puts "#{k.keyword_id} : #{k.freqency}" }
To put this to good use, you can then map out your actual Keyword objects:
class Keyword < ActiveRecord::Base
# other stuff
def self.most_popular
KmRelationship.
select('keyword_id, count(keyword_id) as frequency').
order('frequency desc').
group('keyword_id').
take(20).
map(&:keyword)
end
end
And call with:
Keyword.most_popular
#posts = Post.select([:id, :title]).order("created_at desc").limit(6)
I have this listed in my controller index method which allows the the order to show the last post with a limit of 6. It might be something similar to what you are trying to do. This code actually reflects a most recent post on my home page.

can there be a tooltip with data from the database

I need to know can there be a tooltip populated with data from database to be displayed in the tooltip.
something like the tooltip should contain
name stauts
abc active
xyz active
pqr active
name and status are retrived from db
I need this tooltip onmouseover, am using CJSON decoded to render the content
i did go google but hardly did find that i would throughly understand and implement.
can anyone out there has any ideas for what am looking.
There is a extension named yii-bootstrap, which described clearly here.
For using tooltip easily in this extension, just look here.
I use cluetip for this. Its not related to Yii but will give you some idea :
JS
function renderInfoTips(opts){
var elements=$('#'+opts.form).get(0).elements;
for(i=0; i<opts.tips.length;i++){
$(elements[opts.tips[i].field]).parent().prepend(opts.tips[i].tip);
}
var clue_opts={arrows:true,splitTitle: '|',closePosition: 'title',sticky:true,dropShadow:false,mouseOutClose:true,
onShow:function(ct, ci){
if(!$.browser.webkit) $(ct).css('top',$(ct).position().top- 30+'px');
}
}
$('#'+opts.form).find(".infotip").cluetip(clue_opts);
}
PHP
function setInfoTipsJavascript($form_id,infotips){
if (count($this->infotips) <1 ) return '';
//get all tip names
$names_csv=join(',',array_keys(infotips));
//get tips details from db
$query="select name, description from infotips where FIND_IN_SET(name ,'$names_csv')";
//run the query, in Yii you have to use InfoTipsModel , I have skipped that portion
//$infotipS , lets say this is query object
$tips=array();
while($tip=$infotipS->Assoc()){
$this->infotips[$tip['name']]['tip']="<a href='javascript:void(0)' class='infotip' title='|{$tip['description']}'> </a>";
$tips[]=$this->infotips[$tip['name']];
}
$tips=json_encode($tips);
$script="\nrenderInfoTips({\"form\":'{$form_id}', \"tips\":{$tips}});\n\n";
echo $script;
}
I am sharing this hoping this will give u some idea. Its obvious you have to : create infotips table, a model for that, and create a widget etc to fetch infotips related to your form fields . As someone suggested, if you are using Bootstrap, you have better way to do that.

How to Get a data from a table and insert another table in YII form

I am newbe in YII. Apologies for simple and stupid question.
I messed up my system. In that Case I need your help to clear the concept :: my question is :::
How to Get a data from a table and insert another table in YII form ::::
means---> I want to get data and view in form field --> how can I do that ??
please help me out
Generate a model using gii. It will do all what you need and hopefully you can get the code for that.
localhost:2010/index.php?r=gii
Use Above changing your host and port to see Gii.
You have to Enable Gii in the Config file before doing this
$modelA = ModelA:;model()->find('somecondition');
$modelB= new ModelB();
$modelB->attribute1 = $modelA->attribute1;
$modelB->attribute2 = $modelA->attribute2;
$modelB->attribute3 = $modelA->attribute3;
$modelB->attribute4 = $modelA->attribute4;
$modelB->attribute5 = $modelA->attribute5;
$modelB->save();
Try this..

Add data from one table to another in ruby on rails

In my current project, I have to get some data in a column of one table and put them to the 2nd table. The first table data have been saved as hash as follows:
---
- werweqr
- test
- B1
- B2
- B3
- xvxczv
I write the following code in the migration file to add the data from the first table to the 2nd table. But the data are not sending from the first to second.
#scenario_response = ScenarioResponse.where("selected_barriers != ?", "");
#scenario_response.each do |p|
p.selected_barriers.each do |barrier|
Settings.test = barrier
# SelectedBarriers.create(:scenario_response_id => p.id, :barrier => barrier)
end
end
Can anyone please let me know if there's something wrong in my code.
If so how to fix it?
Thanks a lot
I don't think you need to call "each" on p.selected_barriers.Try removing the each and doing this:
Settings.test=p.selected_barriers.
I'm new to RoR too..According to me,the scenario_response is a collection that returns all instances that have the selected_barriers as "". Since you are doing an each on the collection, you will just have one selected_barriers item for each of them.
Please try this and let me know if I'm wrong.
Also you are not doing update_attributes.
Try doing Settings.update_attributes(params[:test]) after Settings.test = barrier .