activerecord

Rails 3 - Potential Race Condition?

非 Y 不嫁゛ 提交于 2019-12-11 07:38:16
问题 I have a really simple Rails 3 application where users can reserve one of a finite number of homogeneous items for a particular day. I'm trying to avoid a race condition where two people reserve the last item available on a particular day. The model (simplified) is as follows: class Reservation < ActiveRecord::Base belongs_to :user attr_accessible :date MAX_THINGS_AVAILABLE = 20 validate :check_things_available def check_things_available unless things_available? errors[:base] << "No things

How is it possible that SQL query and ActiveRecord.find_by_sql return different results?

倾然丶 夕夏残阳落幕 提交于 2019-12-11 07:34:24
问题 I have a conundrum. I am writing a scope on my Account class that finds a bunch of account objects that meet a lengthy condition. Here is my scope: scope :unverified_with_no_associations, -> { find_by_sql("SELECT COUNT(DISTINCT(accounts.id, accounts.email)) FROM accounts WHERE level = 0 AND id NOT IN (SELECT DISTINCT(account_id) FROM verifications) AND id NOT IN (SELECT DISTINCT(account_id) FROM positions) AND id NOT IN (SELECT DISTINCT(account_id) FROM edits) AND id NOT IN (SELECT DISTINCT

Display Last Child in React Iteration

冷暖自知 提交于 2019-12-11 07:33:35
问题 I'm trying to implement a message app, without ActionCable for now. When I map over all messages, I want to show only the last message from that user: instead of showing all messages, from a user, I only show the last. I do not know how to achieve that. I thought I could set the key to the user_id instead of the id . _messagesRender: function(){ var messages = this.props.message.map( function(m, i){ return( <li key={m.user_id}>{m.body}</li> ) }); return( <div>{messages.length === 0 ? "No

How do you handle side effect of duplication when using Joins tables - i.e. reciprocated friendships etc

↘锁芯ラ 提交于 2019-12-11 07:30:57
问题 Using join tables in rails always casuses problems for me especially with having to create duplicate entries when reciprocration is assumed. I.e. i add someone as a friend and i automatically become theirs == two joins required. Take a simple receipts app that keeps track between two people, who spent what. A USER can have multiple FRIENDS. For each FRIEND they can track multiple TABS ( i.e. a tab for groceries, another for bills etc) each with multiple RECEIPTS added by either USER. So in

Ruby on Rails 3 - Serialize Array Mismatched Class Bafflement

蓝咒 提交于 2019-12-11 07:28:34
问题 Hey there, I am baffled by this serialization mismatch problem in Rails 3.0.5 and Ruby 1.9.2. I am seeding the database with a subclass of Array and then trying to save to an ActiveRecord object. Can anyone please help me? I was initially trying to serialize as Graph but reduced it to Array to avoid bugs with the custom class. I am very stumped as this doesn't make intuitive sense to me. Thank you very much for your help! class Graph < Array .. class Settings < ActiveRecord::Base serialize

Rails 3 - Easily work with Pascal Case column names

倾然丶 夕夏残阳落幕 提交于 2019-12-11 07:14:30
问题 We are beginning a Rails 3 front end to a newly developed application written in Java w/ SQL Server data layer. Due to the team's previous experience, all column names are in Pascal Case (Id, Name, MyTableId) and table names are in singular form (not pluralized). Is there a way to easily globally overwrite the default rails conventions to work with this data model? I realize this can be done by using set_table_name and column aliasing, but that kind of defeats the purpose of using Rails to

AR get own posts and friends posts

廉价感情. 提交于 2019-12-11 06:59:30
问题 I have these tables; user - contains user_id | username | fullname | email etcc user_followers - contains follow_id| user_id | follower_id | date etcc posts - contains post_id | user_id | post | post_date I’m trying to grab all the posts by the users the user is following and the user’s own posts. I’ve tried $this->db->select('posts.*')->from('posts') ->join('user_followers', 'user_followers.user_id = posts.user_id', 'INNER') ->where('user_followers.follower_id', $user_id)->order_by('posts

What are 'best practices' for dealing with transient database errors in a highly concurrent Rails system?

ε祈祈猫儿з 提交于 2019-12-11 06:57:01
问题 While researching a deadlock issue, I found the following post: https://rails.lighthouseapp.com/projects/8994/tickets/6596 The gist of it is as follows: the MySQL docs say: Deadlocks are a classic problem in transactional databases, but they are not dangerous unless they are so frequent that you cannot run certain transactions at all. Normally, you must write your applications so that they are always prepared to re-issue a transaction if it gets rolled back because of a deadlock . Therefore

Getting MySQL syntax error using CodeIgniter LIKE active record

左心房为你撑大大i 提交于 2019-12-11 06:55:39
问题 This is my code return $this->db ->select('organization') ->like('title',$this->db->escape_str($query)) ->or_like('description',$this->db->escape_str($query)) ->get('shop_search') ->num_rows(); every thing works well until there is a ' and NOT " in the $query . The error is: $query="d'" You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%' OR `description` LIKE '%d\\\\\\'%'' at line 3 SELECT `organization`

Rails: How to create polymorphic relationship/friendship model

假装没事ソ 提交于 2019-12-11 06:53:19
问题 I want to create a polymorphic friendship/network system. For example User can request or be requester to join multiple models like: Company Project Group Contacts(friends) Some will be one to many e.g. a user is employed by one company but a company has many employees. Others many to many e.g a user has many projects a project has many users The relationships aren't to difficult to create using active recorded. The middle step, requesting(invitations) that has me baffled. After bit of