different type of notifications [Rails 4] - ruby-on-rails

I am developing a feeds application with ruby on rails, and I want to show in the main page of the application some courses activities managing by the users.
Basically, the idea is a user can create a course, other users can follow the course and comment inside that event, later in the future, I Want that it will be more complex.
My solution is having a notifications table which manage all this. Above it shows the table which will have a feed type (1. create course, 2. pay the course, 3. comment into this group, 4. course deleted, 5. share course... and could be more). user_id field refiere to the user who should see the notification, message_id not nil only when the notification consist in add a comment.
<<Table>>
Feed
----------------
+ id : integer (Pk)
+ created_at : date_time
+ notification_type : string
+ text : text
+ course_id : integer (Fk)
+ user_id : integer (Fk)
+ comment_id : integer (Fk)
I am wondering if that would be the best implementation of this problem if I Want to make it more complex in the future. Please any feedback is welcome!
Thank you!

You're implementing the observer patter for this application. Based on your requirements, it looks like a fine implementation.
Here are some resources about this pattern.
http://www.sitepoint.com/design-patterns-in-ruby-observer-singleton/
Implementing Observer Pattern for my Rails App

Related

Return only results based on current object for dynamic menus

If I have an object that has_many - how would I go about getting back only the results that are related to the original results related ids?
Example:
tier_tbl
| id | name
1 low
2 med
3 high
randomdata_tbl
| id | tier_id | name
1 1 xxx
2 1 yyy
3 2 zzz
I would like to build a query that returns only, in the case of the above example, rows 1 and 2 from tier_tbl, because only 1 and 2 exist in the tier_id data.
Im new to activerecord, and without a loop, don't know a good way of doing this. Does rails allow for this kind of query building in an easier way?
The reasoning behind this is so that I can list only menu items that relate to the specific object I am dealing with. If the object i am dealing with has only the items contained in randomdata_tbl, there is no reason to display the 3rd tier name. So i'd like to omit it completely. I need to go this direction because of the way the models are set up. The example im dealing with is slightly more complicated.
Thanks
Lets call your first table tiers and second table randoms
If tier has many randoms and you want to find all tiers whoes id present in table randoms, you can do it that way:
# database query only
Tier.joins(:randoms).uniq
or
# with some ruby code
Tier.select{ |t| t.randoms.any? }

Tracking Followers Over Time

I want to build functionality in my Rails application that shows follower trends over time.
Currently, my following methodology involves creating and destroying relationship objects - following creates an object with the IDs of the follower and followed and unfollowing deletes that relationship object.
Since the relationship object is deleted upon an unfollow, it's impossible to go back and look at how many followers existed for a followed at any given time.
To solve this, the best solution I can think of is this:
Instead of deleting a relationship object upon unfollowing, create a new object with a negative value of, say, -1. Following would create an object with a positive value of +1. Therefore, adding up the total values for a given pair would yield whether or not they were currently following (1 or 0), while historical trends could also be calculated by adding up the total following values for a given followed.
My question is: Is this the most elegant solution this problem? Is there an easier way to do it? I realize that it's possible to use cron jobs to output a daily number, but that seems like it would duplicate data. Any other suggestions?
Any help would be greatly appreciated!
I would add an active field then instead of deleting the relationship record I would set the record to inactive. Then you'll have to update all of your user facing queries to reflect active = 1. Then you can use the records with active = 0 for reporting purposes. You can also add a deactivated_at field that stores the date that the record was deactivated.
An example scenario would be user 1 follows user 2, follows user 3, follows user 4, un-follows user 2, re-follows user 2, un-follows user 4.
follower_id followed_id active created_at deactivated_at
1 2 0 9/10/2012 9/13/2012
1 3 1 9/10/2012 NULL
1 4 0 9/10/2012 9/17/2012
1 2 1 9/16/2012 NULL
just use paranoia
https://github.com/radar/paranoia
class Relationship < ActiveRecord::Base
acts_as_paranoid
...
end
(if you have a unique index over the two numeric ID columns, remove it, use a plain index)
Then you can have
def currently_following_count(uid)
Relationship.where(:followed_id => uid).count
end
def historical_following_count(uid)
Relationship.unscoped.where(:followed_id => uid).count
end

Magento: Create an invoice with invoice number = order number?

I am using this code to create an invoice in Magento:
$invoiceId = Mage::getModel('sales/order_invoice_api')->create($order->getIncrementId(), array());
This automatically assigns a number (increment_id) to the invoice, for example 100016050. I want to create an invoice where the increment_id of the invoice = the increment_id of the order.
How can that be done?
Thanks!
This would require coding a complete custom module, so I'll just explain some basics.
In Magento, entities like order, invoice, creditmemo and shipping each have its own and independant number group per store_id.
These number groups can be defined in the table eav_entity_store:
entity_store_id entity_type_id store_id increment_prefix increment_last_id
1 5 1 1 100000000
2 6 1 2 200000000
3 7 1 3 300000000
4 8 1 4 400000000
To know which entity_type_id refers to which entity, check your eav_entity_type table:
entity_type_id entity_type_code entity_model
5 order sales/order
6 invoice sales/order_invoice
7 creditmemo sales/order_creditmemo
8 shipment sales/order_shipment
Note that your entity_type_id's may (or may not) vary from that.
Magento usually increments each of this entities by one, see eav_entity_type.increment_per_store.
This happens the time such entity is created. But, the creation of an order doesn't always mean, that an invoice for it will be created, too. For example the user could cancel the payment while order placing, or the payment will not be authorized by the payment provider, so no invoice would be created.
This may lead to gaps, e.g. order already at 100000005, while invoice still at 200000002.
Your code would need to manage this gap in a way that keeps order and invoice in sync.
To do this, you could create an observer for the sales_order_invoice_save_before event, for example.
app/code/local/Mycompany/Mymodule/etc/config.xml:
<config>
<modules>
<Mycompany_Mymodule>
<version>0.1.0</version>
</Mycompany_Mymodule>
</modules>
<global>
<models>
<mymodule>
<class>Mycompany_Mymodule_Model</class>
</mymodule>
</models>
<events>
<sales_order_invoice_save_before>
<observers>
<myobserver>
<type>singleton</type>
<class>mymodule/observer</class>
<method>salesOrderInvoiceSaveBefore</method>
</myobserver>
</observers>
</sales_order_invoice_save_before>
</events>
</global>
</config>
app/code/local/Mycompany/Mymodule/Model/Observer.php:
class Mycompany_Mymodule_Model_Observer
{
/**
* Hook to observe `sales_order_invoice_save_before` event
*
* #param Varien_Event_Observer $oObserver
*/
public function salesOrderInvoiceSaveBefore($oObserver)
{
$oInvoice = $oObserver->getInvoice();
}
}
Magento passes the invoice object to this observer, before the invoice object is saved. This would allow you to retrieve the related order object (and therefore the order's increment_id) using this invoice object.
Having retrieved the order.increment_id you could search the invoices to find out whether or not an invoice with that order.increment_id already exists.
If it doesn't exist yet, you can assign the value of order.increment_id to invoice.increment_id before leaving the observer and are done.
Please note, that these are only the basics. There are some more pitfalls to it.
For example, multiple and/or duplicate invoices per order cases are not handled yet.
For example, in some countries the fiscal/tax authorities require invoice numbers to be continuously increasing. It must be 1, 2, 3, 4, 5, but 1, 2, 3, 4 is missing, 5 is not acceptable. Using the technique above, such gaps can still happen, because of payment cancellations by the user, etc.
However, this should get you on the right track.

Help me to get better understanding of Digg's Cassandra data model

http://about.digg.com/blog/looking-future-cassandra
I've found this article about Digg's move to Cassandra. But I didn't get the author's idea of Bucket for pair (user,item). Little more details on the idea would be helpful to me to understand the solution better.
Thanks
It sounds like they are using one row in a super column family per user with one super column per item; a subcolumn for an item super column represents a friend who dugg the item. At least in pycassa, this makes an insert as simple as:
column_family.insert(user, {item: {friend: ''}})
They could also have done this a couple of other ways, and I'm not sure which they chose.
One is to use a standard column family, use a (user,item) combination for the row key, and use one column per friend who dugg the item:
column_family.insert(user + item, {friend: ''})
Another is to use a standard column family, use just (user) for the row key, and use an (item, friend) combination for the column name:
column_family.insert(user, {item + friend: ''})
Doesn't sound like this is what they used, but it's an acceptable option as well.

updating wrong value to database

I'm just trying to increment a record by 1 starting at 2000, when a new record is created upon clicking on the create action to create a record:
if resource_model == Student then #resource.testing_id = id + 2000 end
So if the record has an id of 1, I assume that the testing_id will be 2001. But instead it returns:
2147483647 (maximum mysql limit?)
Any suggestions on how to address this? Thanks.
You can't know record ID during create. ID is known after saving record do database.
You can't relay on ID to give you values like 1, 2, 3 ... and so on.
Don't store value like ID+2000, becouse you can get it at any time by calculating id+2000.
You can get next testing_id by something like this:
if resource_model == Student then
#resource.testing_id = Student.first(:order => "testing_id DESC").testing_id + 1
end
But if two processes at the same time will fetch the same value then you will have duplicate testing_id.
Object.idf is a (deprecated) method that returns the Ruby object ID, which uniquely identifies the object. Rails has overridden id so that in models it refers to the database primary key. I'm going to take a guess that your code snippet is from a controller, and that's why id is returning a strange and large number: It's the Ruby object id of the controller, not the primary key of the object. Give id a receiver, e.g. #resource.id, and see how that works.
2147483647 = 2 ^(32-1)
Could you show some of your code here?
From what i'm guessing here is that you are comparing apples with strawberries :P.
I think you are adding a "1" on a string so that means for example:
2000 + 1 = 20001
20001 + 1 = 200001
So if you do that a couple of times this means you will get to the maximum size of an int (21475483647). I don't know this for a 100% sure, but this has to do with the way you ask your question and the way you don't post code etc...
I hope this edit was helpfull tho.

Resources