activerecord

Atomically increment a field using SubSonic 3 ActiveRecord

落爺英雄遲暮 提交于 2019-12-23 03:18:21
问题 I'm tring to increment a field in a MySQL database using SubSonic 3 ActiveRecord. In SQL, this is what I'm after: UPDATE people SET messages_received=messages_received+1 WHERE people_id=@id_to; I tried the following, but it didn't seem to work (messages_received always seemed to be 1): _db.Update<person>() .Set("messages_received").EqualTo(x => x.messages_received == x.messages_received + 1) .Where(x => x.people_id == idTo) .Execute(); This did work: string sql="UPDATE people SET messages

Activerecord find records by time in datetime

ε祈祈猫儿з 提交于 2019-12-23 03:09:13
问题 I am trying to perform an active record query that returns records by time by looking in the datetime field. Specifically I want to get all records that were created between 1am and 6am, not caring what day they were created just making sure that the time falls within the range. So if i had records: 1: car=> "Honda", color: "blue", created_at => "2014-11-13 01:15:00" 2: car=> "Toyota", color: "red", created_at => "2014-11-17 04:15:00" 3: car=> "Honda", color: "red", created_at => "2014-11-20

New record isn't saved and no error messages

拟墨画扇 提交于 2019-12-23 02:51:10
问题 I am trying to save a new row into a quote, but Yii doens't save the QuoteRow. It only saves the new service (Yes the DB-structure is a bit weird). I can't seem to figure it out. If the row doesn't get saved, $qr->save() should return false but it doesn't. The service is successfuly inserted, however the quoterow isn't. $service = new Services; $service->label = $row['title'] ?: "Övrigt"; $service->is_priced_per_unit = 1; $service->price_per_unit = $row['price']*0.8; $service->is_default = 0;

Existing Rails model without fetching it from the database

一个人想着一个人 提交于 2019-12-23 02:28:42
问题 Does anyone know if its possible to create a model instance and apply the ID and any other attributes without having to load it from the database? I tried doing this, but the associations are not fetched from the database :( Any ideas? EDIT What I want to accomplish is simply this: Fetch an existing record from the database. Store as "hashed" output of the record into redis or some other memory store. Next time when that record is fetched, fetch the cached store first and if it is not found

postgresql, odd OFFSET/LIMIT behavior ( records order )

橙三吉。 提交于 2019-12-23 02:24:06
问题 so basically I have this scope(sql): scope.to_sql => "SELECT \"offers\".* FROM \"offers\" INNER JOIN \"cities_offers\" ON \"offers\".\"id\" = \"cities_offers\".\"offer_id\" WHERE \"cities_offers\".\"city_id\" = 2 AND \"offers\".\"category_id\" IN (2) AND (offers.category_id is NOT NULL) ORDER BY offers.discount desc LIMIT 25 OFFSET 0" Somehow the records order is different for the above query and the same one without LIMIT and OFFSET: scope[6] => #<Offer id: 8629 ... scope.except(:offset,

How to Generate Castle ActiveRecord C# Classes for an Existing Database

允我心安 提交于 2019-12-23 02:05:46
问题 What is the best way to generate C# classes for use with Castle ActiveRecord and NHibernate given an existing database structure? Can you recommend any of the class generation tools or is it just less hassle to write the classes by hand? 回答1: If you only have a handful of tables with not many columns, it may be best to write your classes by hand. I wouldn't recommend hand writing many classes though if your database already exists. Active Writer may meet your needs: http://using.castleproject

Yii determining existence of related models

本小妞迷上赌 提交于 2019-12-23 01:40:11
问题 I want to run a findAll query that that only returns records where a related record doesn't exist. Can anyone tell me how this is done in Yii? Just a little bit of context in case it helps- I'm working on a survey application, the objects I'm working with are- QuestionSurvey AnsweredQuestion SurveyQuestion HAS_MANY AnsweredQuestion I therefore want to return QuestionSurvey models where no related AnsweredQuestion exists. Thanks in advance, Nick 回答1: If you SurveyQuestion::model()->with(

Using Rails/AR to generate this complex query

心不动则不痛 提交于 2019-12-23 01:40:11
问题 In trying to solve a grouping and ordering problem (original question here: "Complex" grouping and indexing in rails?), I got a SQL query that will fetch the right records the right way. My question now is: how do I generate this SQL query using Rails/AR synthax? The SQL-query is as follows: SELECT u.id as owner_id, u.name as owner_name, t.id, t.due_date FROM users u INNER JOIN tasks m ON u.id = m.owner_id INNER JOIN tasks t ON u.id = t.owner_id GROUP BY u.id, u.name, t.id, t.due_date ORDER

Getting count by drilling down through relations

五迷三道 提交于 2019-12-23 01:37:10
问题 I'm using Rails 4 and I want to get a count of votes for each individual post review for a given post. Schema: Post PostReview PostReviewVote id post_id post_review_id user_id user_id voted_on_date I have something like: table.table.table-striped.table-hover thead tr th Post Name th Reviews Created For Post th Total Votes in All Reviews For Post tbody - for post in @posts tr td= post.name td= PostReview.where(post_id: post.id).size td= PostReview.where(post_id: post.id).PostReviewVotes.size

Only allow users to create one review per movie

℡╲_俬逩灬. 提交于 2019-12-23 01:21:06
问题 I have my app setup where users can write reviews for a movie. What I'd like to do is limit the user to create only one review per movie. I've managed to accomplish this in my reviews controller as so: class ReviewsController < ApplicationController before_action :has_reviewed, only [:new] .... def has_reviewed? if Review.where(user_id: current_user.id, movie_id: @movie.id).any? redirect_to movie_reviews_path flash[:notice] = "You've already written a review for this movie." end end end Where