Show authorizations according to situations - ruby-on-rails

I have Employee and this have authorizations. How to show only authorizations with situation 1 and 6.
this is my code:
if employee.authorizations.situation == 1
(...)
elsif employee.authorizations.situation == 6
(...)
But, ever return false, but I have in my database 3 authorizations situation 1, and 2 authorizations situation 6.
In console, when I enter employee.authorizations.first.situation == 1, retunr true. Same thing for situation 6.
What is my error? Thanks!

If you want to filter a collection by some property:
employee.authorizations.where(situation: 1)

This is the complete answer
employee.authorizations.where(situation: 1).exist?

Related

Ruby On Rails Active Record - Where Condition AND This Condition or That Condition

I have a simple time tracker and just trying to write a query that satisfies the following and pulls all records with the following: user_id = 6 AND (is_paused is true OR manual_input_hours is nil).
Based on what I tried to structure as follows
Timerecord.where(user_id: 6).where(Timerecord.where(is_paused: true).or(Timerecord.where.not(manual_input_hours: 0)))
but it doesnt work, getting the following error: Unsupported argument type: #Timerecord::ActiveRecord_Relation:0x000056546f549358 (Timerecord::ActiveRecord_Relation) (ArgumentError)
You have the OR part correct, you just need to combine it with the user query. You can do this with the merge method to get the AND behaviour:
records_for_user = Timerecord.where(user_id: 6)
paused = Timerecord.where(is_paused: true)
has_manual_input = Timerecord.where.not(manual_input_hours: 0)
# records_for_user AND (paused OR has_manual_input)
records_for_user.merge(paused.or(has_manual_input))

Validations Custom - Compare two variables between new and existing objects (dates) Rails 5

My rails application is a residence application.
I have a model stay which is a third model in a has_many:through assocation between a tenant and a flat. A stay is defined by a checkin_date and a checkout_date. I would like to create a custom validations to prevent the creation of a new stay if there is already a tenant in a flat for a given period...but i never write a custom validation before so i'm super lost...
By logic, i know i need to compare the checkin_date and checkout_date of the "already existing record" and the "supposed new one". So I guess it would look like : (n+1= the new record vs. n= already existing record)
> def duplicate_stay
>
> if Stay.exists?(tenant_id: current_tenant.id, studio_id:
> current_studio.id) &&
> checkin_date(n+1) > checkin_date(n)
> checkout_date(n+1) > checkin_date(n)
> checkin_date(n+1) < checkout_date(n)
> checkout_date(n+1) < checkout_date(n)
> == false
> else
> == true (the model can be created)
end
Can someone help me? I keep looking but I know understand how to do it !
Try using the cover? method. You can use it like this:
today = Date.today
in_one_week = today + 7.days
search_date = today + 3.days
(today..in_one_week).cover? search_date
=> true
This is not tested, but if you added a scope to the Stay class:
scope :overlaps, ->(stay) {
where(
tenant_id: stay.tenant_id,
studio_id: stay.studio_id,
).
where(arel_table[:checkin_date].lt(stay.checkout_date)).
where(arel_table[:checkout_date].gt(stay.checkin_date))
}
... then for an instance of Stay #stay calling Stay.overlaps(#stay).exists? would return true if there was any instance that overlapped with #stay.
Let me know if that doesn't work. It's often helpful to look at the SQL that is executed as part of a validation if you're not sure why something isn't working the way you think it should.
Edit: one tricky thing here is that a stay overlaps with itself, so if #stay was saved successfully (ie. it has an id assigned) you might have a problem with that logic. You can provide different logic depending on whether the record is saved or not, though.
I think that this would do it:
scope :overlaps, ->(stay) {
logic = where(
tenant_id: stay.tenant_id,
studio_id: stay.studio_id,
).
where(arel_table[:checkin_date].lt(stay.checkout_date)).
where(arel_table[:checkout_date].gt(stay.checkin_date))
logic = logic.where.not(id: stay.id) unless stay.new_record?
logic
}

Grails GORM behaving strangely

I am experiencing the following very strange behavior with Grails GORM queries...
NOTE: I have 20 instances of MyDomain created. I.e 20 records
CASE 1
List<MyDomain> case1Results = MyDomain.where {
(isTypeA == true) && (relation1.relation2.relation3.organization == org)
}.list()
In this case, case1Results is an empty list.
CASE 2
List<MyDomain> case2Results = MyDomain.where {
(isTypeA == true)
}.list()
In this case, case2Results contains the 20 records I have stored.
This all seems to find up to this point. This shows that the 20 records are not associated with organization. However...
THE PROBLEM:
when I take case2Results and do the following...
println(case2Results.every {
it.relation1.relation2.relation3.organization == org
}) //true
I get true
OR:
case2Results = case2Results.findAll{
it.relation1.relation2.relation3.organization == org
}
Here, case2Results still contains 20 records as in CASE 2 above, not 0 as in CASE 1
I can't wrap my head around this behavior and I don't even know how to explain this if not with these examples. Does anyone know why this is happening and how to fix it?
PS: Each of my domain classes has a belongsTo and hasMany static maps, creating relationships between the domain classes.
PS: This behavior is being experienced when I run unit tests but works well in the actual feature being tested.

Left join on 2 foreign keys with Rails and active record

I have 3 objects :
Question
- id
- question
Answer
- question_id
- user_id
- answer
User
- id
One Answer belongs to one User and one question.
I would like to get ever every questions and their associate answer.
And I want answers for a specific user.
I'have tried both query but no one is working as expected.
In this case, if a user answer to the questions. The second user querying will have a return with nothing
##questions = Question.includes(:answer).where(answers: { user_id: [#user.id, nil] })
This one returns the expected data with postgres, but then Rails is making a request for every question to get the answer but without using the condition on the user_id
##questions = Question.joins('left join answers on answers.question_id = questions.id and answers.user_id = #user_id')
I would like to produce this kind of Json for every User.
[
{
id: 1,
question: 'question',
answer: { id:22, answer: 'answer' }
},
{
id: 2,
question: 'question2',
answer: { id:23, answer: 'answer' }
},
{
id: 3,
question: 'question3',
answer: null
},
]
Any help is very welcome.
I can't figure this out

How do I delete a relationship using Neo4jClient

Am trying to get the basics down before moving forward with neo4j. Love the querying aspect but now trying to delete using neo4jclient and stuck.
Simple setup
root-[:has_user]->user
and
user-[:friends_with]->friend`
For a user with the Id of 1 I would like to remove the specified from Id == 2. User 1 is no longer friends with User 2 :(
Anyway, using neo4jclient I first check to make sure that the users are friends in the first place with this:
if (client.Cypher.Start("root", client.RootNode)
.Match("root-[:HAS_USER]->user-[:FRIEND]->friend")
.Where((UserNode user, UserNode friend) => user.Id == 1 && friend.Id == id)
.Return<Node<UserNode>>("user")
.Results
.Count() == 1)
{
now I'm trying to delete:
client.Cypher.Start("root", client.RootNode)
.Match("root-[:HAS_USER]->user-[r]->friend")
.Where("user.Id = 1")
.And()
.Where("friend.Id = " + id)
.And()
.Where(string.Format("type(r) = 'FRIEND'"))
.Delete("r");
}
No errors, but the relationship is still there. Any ideas?
Update 11/12/2012
Got it working. I first updated by Neo4J instance with the stable 1.8. I think something with the latest neo4jclient and neo4j server were not working together. I first got the user's node based on the id, then from that node tested if the node had a relationship, then was able to remove it. Code below:
var currentUserNode = client.Cypher.Start("root", client.RootNode)
.Match("root-[:HAS_USER]->user")
.Where((UserNode user) => user.Id == 1)
.Return<Node<UserNode>>("user")
.Results.Single();
if (currentUserNode.StartCypher("user")
.Match("user-[r]->friend")
.Where("friend.Id = " + id).And()
.Where("type(r) = 'FRIEND'")
.Return<Node<UserNode>>("user")
.Results
.Count() == 1)
{
currentUserNode.StartCypher("user")
.Match("user-[r]->friend")
.Where("friend.Id = " + id).And()
.Where("type(r) = 'FRIEND'")
.Delete("r").ExecuteWithoutResults();
}
One way is to switch to use a CypherFluentQuery instead:
new CypherFluentQuery(client)
.Start("root", client.RootNode)
.Match("root-[:HAS_USER]->user-[r]->friend")
.Where("user.Val = 1").And()
.Where("friend.Val = " + 2).And()
.Where("type(r) = 'FRIEND'")
.Delete("r").ExecuteWithoutResults();
which will do what you want.
I believe this all stems from a bug: https://bitbucket.org/Readify/neo4jclient/issue/40/should-be-able-to-add-cypher-delete-clause
As to why client.Cypher.Start isn't working as it should, I'm not sure, the bug is fixed, and should be working from version 1.0.0.479 (current at time of writing is 1.0.0.496)
Use any Neo4jClient build after 1.0.0.500. See issue 45 if you're interested in why.
Don't forget to call ExecuteWithoutResults. The first code sample in your question is missing this. This is documented on https://bitbucket.org/Readify/neo4jclient/wiki/cypher

Resources