Check for duplicate record and then create - ruby-on-rails

I have an rails app wherein am trying to either downvote or upvote an contact. I have a user model powered by devise. I have an feedback model too in place which stores which user has upvoted/downvoted an contact. How do I get to insert record into the feedback table when two users are trying to upvote/downvote the same contact details?

Contract.find_or_create_by(name: 'xyz') do |contract|
contract.user_id = current_user.id
end
In the above code you have two case
Case 1) Contract name xyz exists in this case that contract will be updated with a id of current user
Case 2) Contract name xyz doesn't exists in this case a new contract will be created with a id of current user
More information can be found in Api Dock

Related

Attaching existing order (order.email) to create an account, Devise?

This is the scenario I would like to accomplish:
User creates an order (enters email into order table).
User is sent order confirmation email with link to sign up.
If User decides to sign up, it will connect to the account.
What would it take to accomplish this?
I'm a bit mixed up because the Order is already created and wouldn't have a current_user for the order model to attach to the User model. How would I then have it so the order.buyer_id (which is used for the current_user of the User who creates the Order) then gets associated with the to-be-signed up User who just created the order. How could I somehow embed this information in the "sign up" link that gets sent? To somehow say "if this email signs up and is confirmed, become the buyer_id for said order"?
Also, is this good practice?
Other option:
Or should I just have a "email" and "password" field when checking out where the User field get sent ahead of the payment token so the order attaches to the current_user?
Does anyone have other ideas or what I should do?
Other than this, the only thing I'm currently doing is a simple "sign up before ordering if you want to save your orders"
Email is unique for each user so after user signed up and account created you can run a small query where you assign created user to orders. It can be accomplished in the model
class User < ActiveRecord::Base
after_create :connect_orders
...
def connect_orders
Order.where(email: self.email).updated_all(buyer_id: self.id)
end
end

Unique Session ids in Rails

I made a shopping cart model for a webapp that I am working on. It was just a standard ruby on rails model.
It lets users add products to the shoppingcart, but only shows it to them if they are the user that added the item. However, I'd like to let users who haven't signed in add items to a shopping cart.
Right now I'm using devise so I can check for a current_user, and assign that shopping cart item to that user. However is there a similar unique session id or anything I can use to simulate a user. So I only show the items that were added to the one person?
The reason for this is I'd like my user to go through as much of the ordering process as possible before asking them to sign-in and make an account.
Thanks
A simple method for this is creating a shopping cart and then adding that identifier to the session regardless of their logged in state.
For example:
#shopping_cart = ShoppingCart.create(user: current_user)
session[:shopping_cart_id] = #shopping_cart.id
Later you can retrieve the current shopping cart, if any, as a before_action handler.
I worked on similar problem. What I did was, I used session_id. First, you need a table(say, product_list) to hold the product list for temporary purpose. Populate each product to this table along with session_id. This will help you to identify the association of product_list with the active user. On checkout copy the record from product_list to your shopping_cart table and delete the record from product_list.
Try this, it might help you with the current problem.

Rails Security - Enforcing ownership at the model level

I recently coded up a 'friend' capability with my website. The way it works is if a user wants to 'friend' another user, sending a request creates a user_connection record with the original user set at the user_id and the requested user set as the contact_id. If the other user accepts the request, then another user_connection record will be made, with the user_id and contact_id reversed. If both user_connections exist, then the two users are considered friends. This currently gives each user access to any buildings shared between the two users.
I was wondering if there was some way I could enforce my user_connection model to make sure that whoever is creating the record gets set as the user_id. Otherwise it seems that someone could spoof the user_connection create request to make themself a contact of whomever they want, and then could spoof building shares using the same methodology. Are there any built in methods to prevent this?
My first thought was to have an initializer that always set the user_id to the current_user's id, but it appears that current_user is outside of the context of the model.
Don't allow user_id to be provided as a parameter, using strong params.
So, you could create the relation like that:
#friendship = current_user.friendships.new(contact_id: other_user.id)
Also make sure you provide the correct condition for current_user.
That's it... user_id is implied but never provided.

Pass Account ID to New User During Sign Up

Currenntly, my application is designed using Devise for authentication. I have it so when the first user signs up, an account is created in an Accounts table and the account_id is passed to the User table. I also have it set so that each time a new account is created that user is tagged as an admin. Finally, I have it working where the admin can create new users.
My problem is that at the time the new users are created I need to have these users be assigned the same account_id as the admin to tie the users together. I can do this if I add an account_id field on the form and have the admin manually enter it. What I want to have is that this is automated in the background.
I tried many varieties without success. This is one of the unsuccesful attempts where I put the following in the user.rb
before_save :add_account_id_from_parent
def add_account_id_from_parent
return true unless self.users.present?
self.users.update_attribute(:account_id, 1)
end
I used the number "1" just to see if I could get anything automated and placed in that field.
Like I said manually everything works, but I want it so the acocunt_id is automatically added during sign up based on the admins account_id.
I'm a bit confused why you are calling self.users. If I understand correctly, you want to assign account_id to 1 after a new user is created (as a test). You can do that like this:
before_save :add_account_id_from_parent
def add_account_id_from_parent
self.account_id = 1
end
You don't need to actually update the record since this is assigned before save, and save will write the new value to the db.
Again I might be missing something, if so please clarify.
UPDATE:
If you're validating that account is present, you'll need to change the callback to a before_validation instead of before_save, like so:
before_validation :add_account_id_from_parent

Rails 3 - How to Create a Record if One Does not Exist OR Update the existing records

I'm creating a permissions table for People & Books
In the permissions table I have: Permission.ID, user.id, book.id
I want an admin to be able to set permissions for Users<>Books.
When the user selects the permissions and clicks submit, in the Rails controller, should I be submitting to /create or /update?
is there a way I can submit to just one whether it's new or an update, and let Rails know to either Create or Update a record based on if a record exists per a UserID And BookID?
Thanks
If they're editing an existing record just submit to update. If they are creating a new record and you're making sure there aren't any duplicates, try using find_or_create_by inside your create method.

Resources