How can I select desired columns without getting ActiveModel::MissingAttributeError? - arel

I am trying to retrieve 2 columns from a mysql database to use with FullCalendar, using Ruby but I keep getting the ActiveModel::MissingAttributeError. What is the correct way of doing this? I have tried the following and checked SO but no luck. Please help me. Thanks.
Request.select("title, start").where(:id => '110012')
Request.find(:all, :select => "title, start", :conditions => {:id => '110012'} )

I was able to solve the problem by doing the following. Hope this helps someone else.
#title = []
#start = []
Request.where(:vid =>'111349').find_each do |vacation|
#title.push vacation.title
#start.push vacation.start
end

Related

Creating URL and params in Yii2

I'm trying to create Url with multiple param using Url::to() method but it's not working i also tried UrlManager->creatUrl() method but no luck, i have pretty Url enable and if i use only one query param it works fine but when i try to add more i got error, below is my code.
$linkUrl = \Yii::$app->UrlManager->createUrl(['sell/updatecategory', ['draftId'=> $model->draftId,'catid' =>$model->category_id]]);
<?= Html::button('Change category or product',['value'=>$linkUrl, 'id' => 'StartOver']) ?>
another try is:
$linkUrl = Url::to(['sell/updatecategory', ['draftId'=> $model->draftId,'catid' =>$model->category_id]]);
in the two case above the url output always look like this:
GET http://http://192.168.199.128/trobay/products/sell/updatecategory?1%5BdraftId%5D=20&1%5Bcatid%5D=50
and the server throw an error cannot resolve the url, what i want is something like this:
GET http://http://192.168.199.128/trobay/products/sell/updatecategory?draftId=20&catid=50
The system added some character which i guess is the cause of the problem but don't really know how to remove this. i hot anyone could help with this thanks
Don't use nested array for parameters.
Structure should look like this:
[0 => route, param1 => value1, param2 => value2, ...]
so in your case
$linkUrl = Url::to([
'sell/updatecategory',
'draftId' => $model->draftId,
'catid' => $model->category_id
]);

Sequel Left Join

My aim is to build a relationship between rooms, people and work shift. So my sequel string looks like this :
x = DB[:raum].join_table(:left, DB[:platz], :rid => :id)
.join_table(:left, DB[:patient_behandlungs_link], :platz_id => :id)
.join_table(:left, DB[:patienten], :id => :patienten_id)
.join_table(:left, DB[:behandlungsverfahren], :id => :t2__behandlungsverfahren_id)
.join_table(:left, DB[:dialysezeit], :id => :t2__dialysezeit_id)
.join_table(:left, DB[:nadeln], :id => :t2__dialysenadel_id)
.join_table(:left, DB[:dialysatorzugang], :id => :t2__dialysatorzugang_id)
.where("raumnummer = ?", raumid.to_i)
It's working like this but in the resulting table there is also a field for the shift id. In this state it does not differentiate in which workshift the person is working. if i do a foreach and push the values out I get my empty nil fields with no one inside, which I want to, and I get the people which are in room raumid from all workshifts.
If I make a .filter(:schicht_id => 1) for example, then I loose my nil values. I need them to assign new people to the empty slots, so I tried (:schicht_id => 1).or(:schicht_id => nil) and similar things but I don't get my result, I want
I think i blamed sequel for something that is not related to sequel.
In the image my select box starts showing options from the second value
This behavior made me think, something was wrong with my joins and group by instructions.... i now have to figure out why the select box shows me from values form 2-9 and not from 1-9. In the HTML Site Source code all 9 Options are given.
This is strange for me.
sorry for blaming sequel.

Yii2 load all models automatically

I don't like to declare every model as
use app\models\Country;
Really it bothers me a lot. I like the approach used in Yii 1.15 when you could load all models using import instruction in config like:
'import' => array(
'application.models.*',
)
Yes, I know it's not good for performance. But I have not more than 50 models and I care much more about my own performance rather than of performance of machine.
I had no luck in figuring out how to do it Yii2.
All I found out is that it should be done via bootstrap option in main config file.
I tried the following:
$config = [
'language' => 'en',
'id' => 'basicUniqueApp',
'basePath' => dirname(__DIR__),
'bootstrap' => [
'log',
'app\models\*'
],
But it's not proper syntax.
Any ideas?
You're trying to break down PHP namespace. That's not a good idea.
If you don't want declare on top model, You can call directly without declare like this:
$country = new \app\models\Country();

Rails 2.x query using IN with other conditions doesn't work as expected

I'm trying to write a Rails 2.1 query(I know that I should upgrade, but that's not my decision at the moment) which using the 'IN' condition. It doesn't seem to work as expected.
It only takes into consideration the roles_id = 21 based on the below query and it doesn't consider both the roles_id(i.e., 21 and 31) to display the correct count.
Admin.find(:all, :conditions => ["test_column1 = ? and roles_id IN (?) and test_column2 = ?",234, '21,31', 1]).count
Could one please tell me how can I get this right?
Thank you.
You need to pass an array I think
Admin.find(:all, :conditions => ["test_column1 = ? and roles_id IN (?) and test_column2 = ?",234, [21,31], 1]).count

Make a Mysql query that finds data just before another (in RoR)

I'm making this query to MySql
Image.find( :all,
:conditions => ["created_at > ? && approved = 1", #image.created_at],
:order => "created_at DESC", :limit => 5)
However, I want the images create just before the given image was created at. Right now, it's returning a list of images from the top of the list, that were create much, much before that image. How can I do this?
Your current query will find any images newer than #image because your using >. You'll need to decide what kind of range you're wanting to look in.. What time frame do you consider "just before"? Minutes? Seconds?
To find all images created within 5 mins of #image, try:
Image.find(:all, :conditions => ["(created_at > ? and created_at < ?) and approved = 1", #image.created_at.advance(:minutes => -5), #image.created_at])
My solution feels more like a hack, but I'm not sure of a better one. I set :order => "created_at ASC"... and then reversed the resulting array, and got the response I wanted. Hrm.