rails-activerecord

Rails: Query to sort record by number except 0

感情迁移 提交于 2019-12-31 05:06:09
问题 I'm arranging the data based on a priority(ascending order), where '0' ignored in prioritising. Below is the Rails Query: Profile.where(active: true).order(:priority).pluck(:priority) This query returns an ordered list of records with priorities that starts from '0' [0, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 4, 4, 7] Could you help me figure out how to order the data where the record with "0" is added to last in the query as per the example below. Example: [1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 4, 4, 7, 0]

Active Record where join table record doesn't exist

大憨熊 提交于 2019-12-30 14:10:34
问题 I am trying to get a list of all records that don't exist in a join table. The models are User, Game and MarkedGame, where users can mark games as played. It's a many to many relationship: User > MarkedGame < Game What I want is a list of all games that haven't been marked by the user. I know that I could do two separate queries and subtract them: Game.all - current_user.games But I don't like that this leaves me with an array rather than an Active Record relation object. Plus it seems like

Rails conditional validation in model

时间秒杀一切 提交于 2019-12-30 08:14:16
问题 I have a Rails 3.2.18 app where I'm trying to do some conditional validation on a model. In the call model there are two fields :location_id (which is an association to a list of pre-defined locations) and :location_other (which is a text field where someone could type in a string or in this case an address). What I want to be able to do is use validations when creating a call to where either the :location_id or :location_other is validated to be present. I've read through the Rails

ActiveRecord loads binary field incorrectly on Heroku, fine on OSX

僤鯓⒐⒋嵵緔 提交于 2019-12-30 06:20:09
问题 I have a rails 3.1 app that stores images in a binary field in a postgresql database (I am aware of potential issues with storing images in a database, but have to do so for now). Everything works fine locally in development mode and specs on OSX, but all images are broken in the app deployed to Heroku. I've verified that the data in the database is correct by pointing my local machine at the same database that the heroku instance uses, and all images displayed correctly. So, the problem

ActiveRecord loads binary field incorrectly on Heroku, fine on OSX

六月ゝ 毕业季﹏ 提交于 2019-12-30 06:20:07
问题 I have a rails 3.1 app that stores images in a binary field in a postgresql database (I am aware of potential issues with storing images in a database, but have to do so for now). Everything works fine locally in development mode and specs on OSX, but all images are broken in the app deployed to Heroku. I've verified that the data in the database is correct by pointing my local machine at the same database that the heroku instance uses, and all images displayed correctly. So, the problem

In Rails 4.1, how to find records by enum symbol?

非 Y 不嫁゛ 提交于 2019-12-29 16:08:30
问题 Assume I have this model: class Conversation < ActiveRecord::Base enum status: [ :active, :archived ] end How can I find all active conversations without using the numeric value of the enum or without having to iterate over each conversation? I tried doing Conversation.where(status: :active) , but it didn't yield any results. The only solution comes to mind is to iterate over all conversations and select the active ones, but it doesn't look like a good solution. Conversation.all.select {

Changing table name at query run time in a Rails application

偶尔善良 提交于 2019-12-28 19:34:31
问题 I have a fat multi-tenant Rails app on Apache + mod_passenger that outputs product prices from a PostgreSQL table as follows: Table "public.products" Column | Type id | bigint name | character varying(100) price | numeric(8,2) Then inside products.rb I have... class Product < PostgresDatabase self.table_name = "products" # ... yadda yadda end What I want is to partition the "products" table in a very specific way so that I end up with something like products_TENANT-ID for each tenant

How to add conditional where clauses in rails

无人久伴 提交于 2019-12-28 13:51:09
问题 I am a rails newbie and am trying to perform a search on a table with rails, and i'm just using my sql knowledge to do this. But this just doesn't seems like rails or ruby even... Is there any better way to do what i'm doing below? (basically, only pass date arguments to sql if they are filled) def search(begin_date=nil, end_date=nil) subject = " and created_at " if !(begin_date.nil? || end_date.nil?) where_part = subject + "BETWEEN :begin_date AND :end_date" else if (begin_date.nil? && end

Creating a nested model instance (has_many relation) on the view of its parent model

爷,独闯天下 提交于 2019-12-25 12:48:25
问题 I have a model A und a model B and the relation is A has_many B (and B belongs_to A). I have a model, a controller and views for A and only a model for B. I want to create and edit the instances of B on the edit view of A ( url/a/1/edit). I know I can create a controller for B and call those methods using a form in the view of A, but then I need to redirect back to the view of A, because I don't want actual views for B. Is there a recommended way to do this? What I want is to not break any of

How can I get the datatype of an attribute from a Rails model?

空扰寡人 提交于 2019-12-25 07:29:44
问题 I am using Postgres in a Rails project and I have discovered that I need to change all of my varchar datatypes to citext . Rather than do this by hand, I want to just create a migration that loops through all of the models and their attributes and converts them as necessary. Most of these models are empty, so it's not a matter of instantiating them. I need to find out if ActiveRecord "knows" what the datatype of its corresponding database column is. 回答1: @model.column_for_attribute('title')