I have a system that has a User, Message, and MessageToken models. A User can create Messages. But when any User reads the Messages of others a MessageToken is created that associates the reader (User) to the Message. MessageTokens are receipts that keep track of the states for the user and that particular message. All of my associations in the Models are set up properly, and everything works fine, except for structuring a very specific query that I cannot get to work properly.
User.rb
has_many :messages
Message.rb
belongs_to :user
has_many :message_tokens
MessageToken.rb
belongs_to :user
belongs_to :message
I am trying to structure a query to return Messages that: Do not belong to the user; AND { The user has a token with the read value set to false OR The user does not have a token at all }
The later part of the statement is what is causing problems. I am able to successfully get results for Messages that are not the user, Messages that the user has a token for with read => false. But I cannot get the expected result when I try to make a query for Messages that have no MessageToken for the user. This query does not error out, it just does not return the expected result. How would you structure such a query?
Below are the results of my successful queries and the expected results.
130 --> # Messages
Message.count
78 --> # Messages that are not mine
Message.where.not(:user_id => #user.id)
19 --> # Messages that are not mine and that I do not have a token for
59 --> # Messages that are not mine, and I have a token for
Message.where.not(:user_id => #user.id).includes(:message_tokens).where(message_tokens: {:user_id => #user.id}).count
Message.where.not(:user_id => #user.id).includes(:message_tokens).where(["message_tokens.user_id = ?", #user.id]).count
33 --> # Messages that are not mine, and I have a token for, and the token is not read
Message.where.not(:user_id => #user.id).includes(:message_tokens).where(message_tokens: {:user_id => #user.id, :read => false}).count
Message.where.not(:user_id => #user.id).includes(:message_tokens).where(["message_tokens.user_id = ? AND message_tokens.read = false", #user.id]).references(:message_tokens).count
The Final Expected Result
52 --> # Messages that are not mine and: I have a token for that is not read OR I do not have a token for
My best attempt at a query to achieve my goal
64 --> # Wrong number returned, expected 52
Message.where.not(:user_id => #user.id).includes(:message_tokens).where(["(message_tokens.user_id = ? AND message_tokens.read = false) OR message_tokens.user_id <> ?", #user.id, #user.id]).references(:message_tokens).count
The problem lies in the query trying to find Messages that are not the users and that the user does not have a token for
63 --> #This does not yield the expected result, it should == 19 (the number of Messages that are not mine and that I do not have a token for)
Message.where.not(:user_id => #user.id).includes(:message_tokens).where.not(message_tokens: {:user_id => #user.id}).count
Message.where.not(:user_id => #user.id).includes(:message_tokens).where(["message_tokens.user_id <> ?", #user.id]).references(:message_tokens).count
How can I solve this?
If you don't mind using 2 queries, a possible solution would be:
messages_not_written_by_user = Message.where.not(:user_id => #user.id)
messages_already_read_by_user = Message.where.not(:user_id => #user.id).includes(:message_tokens).where(message_tokens: {:user_id => #user.id, :read => true})
messages_not_read_by_user_yet = messages_not_written_by_user - messages_already_read_by_user
I would personally find this syntax more readable:
messages_not_written_by_user = Message.where.not(:user => #user).count
messages_already_read_by_user = Message.where.not(:user => #user).includes(:message_tokens).where(message_tokens: {:user => #user, :read => true}).count
One remark to this query:
63 --> #This does not yield the expected result, it should == 19 (the number of Messages that are not mine and that I do not have a token for)
Message.where.not(:user_id => #user.id).includes(:message_tokens).where.not(message_tokens: {:user_id => #user.id}).count
This query searches for all the messages which have a token with an arbitrary other user. (If msg1 has a token with #user, and it also has a token with #another_user, this query will find it.)
Full disclosure - I'm not sure how I'd do this as you have it set up right now. However: are you against installing a gem to help? If you're not, I'd suggest you look into the Squeel gem (https://github.com/activerecord-hackery/squeel).
Squeel makes these kinds of associations a lot easier and allows use to use the plain old | operator. It's built on Arel and shouldn't effect anything you've written in ActiveRecord (at least in my experience). Hope that helps!
Ok, so thanks to the help of R11 Runner I was able to come up with a solution, which required using pure SQL. I could not use the Squeel gem or ActiveRecord as there was no equivalent to SQL's NOT EXISTS operator, which was the crucial component missing.
The reason this works is because unlike the other solutions the NOT EXISTS operator will return all records from the Messages table where there are no records in the MessageTokens table for the given user_id, whereas using where.not would look for the first match instead not ensuring the non existence that was needed.
Message.find_by_sql ["SELECT * FROM messages where messages.user_id <> ?
AND (
(EXISTS (SELECT * FROM message_tokens WHERE message_id = messages.id AND user_id = ? AND read = FALSE))
OR
(NOT EXISTS (SELECT * FROM message_tokens WHERE message_id = messages.id AND user_id = ?))
)",#user.id, #user.id, #user.id]
Related
I have the following models:
class Message < ActiveRecord::Base
has_many :read_messages
module Scopes
def by_read_status(read_status, user_id)
case read_status
when 'Unread'
when 'Read'
joins(:read_messages).where(:read_messages => {:read => true, :admin_user_id => user_id})
else
where('1 = 1') # no scope necessary
end
end
end
extend Scopes
end
and...
class ReadMessage < ActiveRecord::Base
has_many :admin_users
has_many :messages
end
I have a controller method called 'mark_as_read' for the message. It just creates a new read_message record with that message id, and the admin user id of the person that marked as read. Since messages are global, I want the ability for each user of the system to manage the read status seperately (which is why I have that extra layer in there). So as you can see by my scope, the by_read_status('Read', user_id) would return all records where it finds a mapped record with read true. The problem is, how can I do the opposite? (return all records where it does not find a map record, OR the map record :read is set to false)?
I am using the scopes like this:
#search = Message.search(params[:q])
messages = #search.result.accessible_by(current_ability)
messages = messages.by_company(session[:company_filter]) if session[:company_filter] && session[:company_filter] > 0
messages = messages.by_campaign(session[:campaign_filter]) if session[:campaign_filter] && session[:campaign_filter] > 0
read_filter = params[:read].blank? ? 'Unread' : params[:read]
messages = messages.by_read_status(read_filter, current_admin_user.id)
messages = messages.page(params[:page]).per(20)
#messages = MessageDecorator.new(messages)
So you can see in the middle there of my scopes, i've got the by_read_status. If I return an array, or something other than a scoped object, it will throw a fit. Can anyone help me figure out how to do the 'Unread' portion of my scope?
Thanks!
Edited Answer
exclude = Message.by_read_status('Read', user_id).map(&:id)
exclude = [0] unless exclude.size > 0
where("id not in (?)", exclude)
where("id not in (?)", by_read_status('Read', user_id))
I'm trying to find out how rails converts a hash such as (This is an example please do not take this literally I threw something together to get the concept by I know this query is the same as User.find(1)):
{
:select => "users.*",
:conditions => "users.id = 1",
:order => "username"
}
Into:
SELECT users.* FROM users where users.id = 1 ORDER BY username
The closest thing I can find is ActiveRecord::Base#find_every
def find_every(options)
begin
case from = options[:from]
when Symbol
instantiate_collection(get(from, options[:params]))
when String
path = "#{from}#{query_string(options[:params])}"
instantiate_collection(format.decode(connection.get(path, headers).body) || [])
else
prefix_options, query_options = split_options(options[:params])
path = collection_path(prefix_options, query_options)
instantiate_collection( (format.decode(connection.get(path, headers).body) || []), prefix_options )
end
rescue ActiveResource::ResourceNotFound
# Swallowing ResourceNotFound exceptions and return nil - as per
# ActiveRecord.
nil
end
end
I'm unsure as to how to modify this to just return what the raw mysql statement would be.
So after a few hours of digging I came up with an answer although its not great.
class ActiveRecord::Base
def self._get_finder_options options
_get_construct_finder_sql(options)
end
private
def self._get_construct_finder_sql(options)
return (construct_finder_sql(options).inspect)
end
end
adding this as an extension gives you a publicly accessible method _get_finder_options which returns the raw sql statement.
In my case this is for a complex query to be wrapped as so
SELECT COUNT(*) as count FROM (INSERT_QUERY) as count_table
So that I could still use this with the will_paginate gem. This has only been tested in my current project so if you are trying to replicate please keep that in mind.
I am trying to receive a JSON post to my Rails 3 application. The JSON post is just an email with a subject which will be one of the following:
BACKUP_PASS/VERIFY_PASS
BACKUP_FAIL/VERIFY_FAIL
BACKUP_FAIL/VERIFY_PASS
etc..
I have the following code in my controller:
def backupnotification
email_payload = JSON.parse(params[:payload])
Activity.create(:action => 'failed to backup', :details => email_payload['recipient'], :user_id => '28')
end
I've also added the following to my routes file:
post '/api/activity/backupnotification' => 'activities#backupnotification'
Obviously, this would create a new Activity record regardless of the backup status. What I would like to do is create an activity with an action of failed to backup if the term FAIL appears anywhere in the subject, and successfully backed up if the term FAIL does not exist.
The JSON post (email_payload) includes an attribute called subject. I was wondering if I could do something like this:
if email_payload['subject'] => "FAIL"
...
else
...
end
What would be the best way of doing this?
Assuming you can access your subject in a similar way as your recipient, you can try something like this.
def backupnotification
email_payload = JSON.parse(params[:payload])
if email_payload['subject'].include?('FAIL')
action_message = 'failed to backup'
else
action_message = 'successfully backed up'
end
Activity.create(
:action => action_message,
:details => email_payload['recipient'],
:user_id => '28')
end
I am working in Rails 2.3.x on a learning management system. Here's my code:
-#users.each do |user|
%tr
%td
=link_to h(user.name), :action => :show_user, :id => user.id
%td="#{h user.grade_level}"
-if QuizResult.find_by_user_id(#user_id).present?
="#{(QuizResult.average('score', :conditions => 'user_id = #{#user.id}') * 100).round}%"
-else
%em No quizzes taken
My quiz_results table has columns for user_id and score, among others. I have a record with a user_id (3907) and result (0.1), and two users I'm looking at with no records in the quiz_results table.
The display says "No quizzes taken" for all three, which leads me to believe that this line is false no matter what:
-if QuizResult.find_by_user_id(#user_id).present?
Any ideas how to fix?
Change #user_id to user.id in the if statement and #user.id to user.id. Also change the single quotations to double quotations or using string interpolation won't work.
I guess you need to change #user.id to user.id.
As the answer to second part of your question, I think you can't have a nested evaluated string (the nested #{}). You need to compute the results first, then evaluate and output it in HAML:
-averge = QuizResult.average('score', :conditions => 'user_id = #{#user.id}') * 100).round
="#{average}%"
i am writing a code to handle read/unread messages, with a simple user_id/message_id mysql table to handle read/unread status.
when the user views the message, i execute
Reading.create(:user_id => uid, :message_id => mid)
there is unique index on user_id/message_id fields combination, so when the entry in Readings already exists, i get ActiveRecord::StatementInvalid error about duplicate entry.
now i could add
unless Reading.exists?(:user_id => uid, :message_id => mid)
Reading.create(:user_id => uid, :message_id => mid)
end
but i imagine this adds one more SELECT query before INSERT
i'd prefer to have just one INSERT, and no error reports even if it fails (i guess REPLACE would be best, but afaik it's not available in ActiveRecord).
Rescue it
begin
Reading.create(:user_id => uid, :message_id => mid)
rescue ActiveRecord::StatementInvalid => error
raise error unless error.to_s =~ /Mysql::Error: Duplicate/
end
This is a bit ugly but will work. Consider tightening up the regex in unless to match out exactly the kind of error you are getting.