I'm writing an app that allows users to feed their inbox, then process their input, making it either a note or a task.
Since I need notes and tasks to behave very differently, I plan on using different models for them.
The question is: how do I go about changing inbox items into notes or tasks?
My first idea was to write an action that would destroy an inbox item and call the create action on the notes (or tasks) controller, but that does not seem right.
Is there a better way to do this?
Update
I'm looking into polymorphic associations as a solution for this, as suggested by #Dipak.
This is my schema: (sorry I can't paste the code. I'm using a web based ssh tool)
I have decided to use the Idea model to define inbox items
And these are my models:
I want to be able to click this link (on my partial)
and have it do two things:
create a task
assign this task to idea.thought
How should I do this? Is this the proper way to use this kind of association?
As per your question it seems like your few initial fields are same for all notes and tasks, that you calling inbox. So better you can save those fields in different table and other fields in related table.
For such situation you should use polymorphic association. You can read about it here. So your new inbox table should be intermediate table used for polymorphic association.
Related
I have a Rails user model with an email field. I want to check whether the user's email is on a list of emails. Let's call it the "Guest List." I'm looking for the cleanest way to keep the guest list and check if the user is on it.
This could be an externally-maintained list (a Google sheet, CSV, etc). Or it could certainly be a table in the Rails app's database.
I was going to create the table, but I don't know the preferred way to do that for such a small task. Should I actually be creating a whole new model for Guest List? It seems like a very high-level thing to create for this one task. The Guest List will never be used for anything other than
if GuestList contains user:email
do something
end
Any advice? Does Rails have some other feature that is good for this?
I have been trying to solve a problem for some time in ruby on rails, but I haven't been able to achieve it and I can't seem to find a solution online (it must be easy but I am not sure what is the write thing to search for)
So, in my web application, I have a CURD table and I use modal to create new items in there:
Image 1
Image 2
This is working perfectly fine. What I would like to do is that when this is created I'd like to post in a different page that " ABC have been created by X User"
In my case that would be the chatbox container:
Image 3
So in my case, the green box is where I would like to say of what has been created and who has created it. I know that it is not a complex problem but I just can't seem to find the solution and I have been trying this for days.
Would really appreciate your help. Please let me know if you don't understand the problem and I can elaborate.
Thanks in advance.
Kind Regards,
Usman
You can create a separate model Notification (or similar name), and use an after_create hook in your original model to automatically create an instance of Notification. The Notification could store the information you want to display as attributes. From there, you'd have to figure out how to display the notification in the correct place on the 3rd page. One approach would be to query all your posts and notifications and sort them by created_at. There's other ways, it's up to you.
I get what you're saying.. but I am a little confused on the first part. So I have a model called Solr that has all the records associated with its User. What do I put in the after_create hook method to display records according to the timestamp in my Chat Box/ Events Log? Do you have an example of a similar implementation?
PS - Why do you say that I need to create a new model why can't I use the same model?
Thanks.
I have tried to search for an answer for this, but nothing seems to be exactly what I'm looking for.
I'm using Rails 4 and I have an Exercise model and a Routine model that have a has_many through relationship via Exercise_Routine. The users have associated with them a list of routines and a list of exercises. The goal is to let a user make up the associations of what exercises belong in routines by being able to drag them from their list of exercises to a list of exercises associated with the routine they're editing.
So it would involve two lists. One being the list of exercises a user has and the other being the list of exercises associated with the current routine. I want to make it so they can drag and drop from either list to edit this or even for the initial creation.
I can get the drag and drop between lists to work. What confuses me is how to do the updating of the database with the changes. Would rails realize that the user just removed one thing from the list and that it should delete that record? Would it try and recreate all the associations in the list every time resulting in an error?
I would also have this happen as part of the edit action inside the routine controller since that is where it logically would make sense to do it. Also would it be better to do an AJAX call that does the update each time they make a change or wait for all the changes and a submit button press to do the actual update?
I can see how I want this to work in my head, but the how is eluding me. Any help is appreciated.
Rails cannot do drag and drop by itself: you need to use some kind of Javascript library like jQuery.
There is a railscast on this topic: http://railscasts.com/episodes/147-sortable-lists-revised?view=comments
I am trying to follow the mantra of "select only what you need" when making database calls in my rails applications. Currently I am eager loading all users against their posts, however I notice that this selects every column from the user table for each users.
I have tried to use a join instead which offers a much easier way to select what specific attributes I need, however I am using paperclip and the user has_attached_file. I currently haven't found a way to include this in the join.
I was just wondering if anyone had any suggestions or tips on what the best way to load both users and posts is in terms of database performance?
Currently to retrieve users I am simply using this sort of syntax:
Post.all.includes(:user)
Have you tried Post.eager_load(:user)? Should fetch all the Post fields and all the User fields in a single query, and instantiate AR objects for each. Paperclip attachment would be stored in the user table under 4-5 fields starting with the same prefix. Those should be loaded through Post.eager_load(:user).
The only reason this wouldn't work is if the association between Post and User is polymorphic.
This is more of an application design question then a specific Rails question. I'm trying to implement a system like "labels" within Gmail. So I want a user to be able to apply a label (or many) to a specific post. However, I also want the user to be able to manage labels using standard Rails CRUD.
Let's say I have a model called Post. So a user has many Posts. A user can create labels which is a separate model from Posts, but these can also be applied to posts with the use of checkboxes in the Post new/edit views. If a user deletes a specific label, it simply gets removed from the Post view and doesn't affect the Post in any other way.
This situation shows my lack of knowledge of db relationships, but is this a case for a has_many_and_belongs_to_many relationship?
If so, would the following design be the way to implement this?
User has_many Posts
User has_many Labels
Post has_many_and_belongs_to_many
Labels
Label has_many_and_belongs_to_many
Posts
Maybe I'm over thinking this and my logic is way off.
You have to decide whether or not you want tags to be "owned" by people or just "used" by people. In gmail, my labels are very specific to me. In SO the tags are not specific to me even though I can create them.
If you want labels as opposed to tags then yes.
User ---< Label
Label >--< Post
User ---< Post
You can define these in English if you want.
A user has zero, one or more Labels
A Label is used by one and only one user
A user has zero, one or more posts
A post is created by one and only one user
A post has zero, one or more labels
a label is applied to zero, one or more posts
I wouldn't say you've over-thought this CONCEPTUAL data model.
From your question, it seems like you are trying to build a taxonomy system (i.e. tagging model). You may want to check out Acts As Taggable On as a possible solution (or just an interesting modeling reference) for your problem.
It sounds like you could implement this functionality through tags. Check out this post for a list of tagging plugins and gems for rails.