activerecord

Bulk updating a joined table with ActiveRecord update_all and Rails 4

眉间皱痕 提交于 2019-12-13 05:16:02
问题 I have a PostgreSQL query that I would like to write in ActiveRecord (Rails 4), but I'm having trouble getting it to work correctly. UPDATE chats AS c SET email = m.source_name FROM messages AS m WHERE c.id = m.chat_id AND m.created_at >= '2014-10-10' This is what I've tried: Chat.joins(:messages) .where("message.created_at >= '2014-10-10'") .update_all('chat.email = message.source_name') But it creates a query like this: UPDATE "chats" SET chat.email = message.source_name WHERE "chats"."id"

rails multiple level include on nested association

穿精又带淫゛_ 提交于 2019-12-13 05:15:19
问题 I have 3 tables, comments, users, and images. I'm trying to formulate a query that will product arrays of comments on a post that includes the commentator's information and a avatar. The avatar is stored in the images table, while the users table contains the user info plus a reference id to the image object that stores the users avatar. Each comment has a author id that is referencing an object in the user table. This is what I have so far @comments = Comment.all(:include => [:user =>

Rails ActiveRecord Eager Load takes a long time

巧了我就是萌 提交于 2019-12-13 04:58:13
问题 I am using eager loading to join two of my tables Model1.eager_load(:model2) The Model1 table has about 800 rows and has many references to other tables. Whenever that line is called, it takes about 3 minutes to load the view that shows the info. I then tried making a straight Postgres connection and running the same SQL query that was generated from the eager load and that finished in 50ms. Why is it taking so much longer in ActiveRecord and is there anyways I can cut down on the time? In

How to show description fields from related table

一笑奈何 提交于 2019-12-13 04:58:11
问题 Just started playing with Ruby (no IT background) and until now went it quite well. But since two days I'm stuck and don't understand what's going wrong... so many thanks already for helping me out with this problem!! The situation is as described below: I created a currencymaster table with the following columns: currmasdesc:string , currmasiso:string . I created a currencyrate table with the following columns: currratemasteridd:integer , currratemasteridc:integer , currraterate:decimal ,

How can I use activerecord's query interface and scopes to identify the favorite in a race

血红的双手。 提交于 2019-12-13 04:44:49
问题 I have two models Starter and Race. Starter belongs_to :race Race has_many :starters The attributes for Starter and race follow: Starter attributes: id, race_id, finish_position, odds Race: id, race_date, race_number, field_size I'm trying to accomplish two things: Select the favorite in each race. #for a particular race the Starter with the lowest odds * Select any beaten favorites *#a favorite that has a finish_position > 1* The logic for determining favorites and beaten favorites is pretty

Ruby on Rails ActiveRecord Database Migration Failure

烈酒焚心 提交于 2019-12-13 04:44:03
问题 I have a preexisting sqlserver database 'MyDatabase' populated with data. Within this database I have two schemas, 'dbo' and 'Master'. dbo is the default schema and contains tables: OWNER LOCATION Master schema contains tables: BANK ZONE Tables OWNER, LOCATION, BANK, and ZONE contain several attributes a piece. I have initialized a RoR server and have verified that the appropriate gems are installed (activerecord, tiny_tds, activerecord-sqlserver-adapter), as well as that in database.yml the

How to create dynamic attribute aliases in rails?

寵の児 提交于 2019-12-13 04:42:10
问题 In class definition I got a list of attributes that I want to return other than database's values unless the container for store of those values is nil: class Label < ActiveRecord::Base CONFIRM_DATA = ["attr1", "attr2"] # "attr1", "attr2" is database fields CONFIRM_DATA.each do |att| alias_attribute "original_#{att}".to_sym, att.to_sym define_method att.to_sym do temp_attr_store[ att.to_sym ] || read_attribute( "original_#{att}".to_sym) end end end So as you see, I try to store in temp_attr

Convert SQL query to Active Record query in Rails 4

本小妞迷上赌 提交于 2019-12-13 04:41:29
问题 SELECT * FROM ( SELECT DISTINCT ON (sec) id, sec FROM tasks ORDER BY sec, id DESC ) sub ORDER BY id DESC LIMIT 4; I am wondering if the above SQL query can be converted to an Active Record query. For now I am using find_by_sql as follows: Task.find_by_sql("SELECT * FROM ( SELECT DISTINCT ON (sec) id, sec FROM tasks ORDER BY sec, id DESC ) sub ORDER BY id DESC LIMIT 4") 回答1: Task.select('t.*').from(Task.distinct(:sec).select(:id, :sec).group(:sec).order('sec, id desc'), :t).order('id desc')

Rails controller use other db for destroy method (update, create not working too, only show)

痴心易碎 提交于 2019-12-13 04:37:49
问题 i have such model: class ToType < ActiveRecord::Base attr_accessible :Name, :TYP_CCM, :TYP_CCM_TAX, :TYP_CDS_ID, :TYP_CTM, :TYP_CYLINDERS, :TYP_DOORS, :TYP_HP_FROM, :TYP_HP_UPTO, :TYP_ID, :TYP_KV_ABS_DES_ID, :TYP_KV_ASR_DES_ID, :TYP_KV_AXLE_DES_ID, :TYP_KV_BODY_DES_ID, :TYP_KV_BRAKE_SYST_DES_ID, :TYP_KV_BRAKE_TYPE_DES_ID, :TYP_KV_CATALYST_DES_ID, :TYP_KV_DRIVE_DES_ID, :TYP_KV_ENGINE_DES_ID, :TYP_KV_FUEL_DES_ID, :TYP_KV_FUEL_SUPPLY_DES_ID, :TYP_KV_MODEL_DES_ID, :TYP_KV_STEERING_DES_ID, :TYP_KV

When calling destroy, record is not being removed from DB

ⅰ亾dé卋堺 提交于 2019-12-13 04:35:14
问题 I've got a link_to meant to destroy a record: <%= link_to 'Delete from DB', {:controller => 'foos', :action => 'destroy', :id => foo.id}, :class => "btn btn-primary btn-mini" %> The destroy method in the controller: def destroy @foo = Foo.find(params[:id]) @foo.destroy respond_to do |format| format.html { redirect_to :back } format.json { head :no_content } end end Pressing the button doesn't delete the record, but if I open up rails console and call .destroy on an existing foo, it is removed