activerecord

Active record update_attribute executes a DELETE query on an associated object(child) of an object(parent) when parent is updated

不打扰是莪最后的温柔 提交于 2019-12-07 21:42:52
问题 Rails: 3.0.11 Ruby: 1.9.3 Why does active record update_attribute executes a DELETE query on an associated object(child) of an object(parent) when I updated the parent? Following are my classes: class User < ActiveRecord::Base has_many :user_keywords, :dependent => :destroy has_many :keywords, :through => :user_keywords end class UserKeyword < ActiveRecord::Base belongs_to :user belongs_to :keyword end class Keyword < ActiveRecord::Base has_many :user_keywords has_many :users, :through =>

NHibernate or FluentNHibernate or ActiveRecord?

时光总嘲笑我的痴心妄想 提交于 2019-12-07 21:15:24
问题 I am in a stage of mapping my CSharp classes into database tables. I have decided to use NHibernate as my ORM tool after comparing with other tools. I have never done a real project with NHibernate before and now am considering alternatives for the mapping, ActiveRecord: according to the project's web site, using ActiveRecord can boost productivity dramatically. However, I do not like the idea of adding attributes to my CSharp classes. After all, my class should NOT have any knowledge of

DISTINCT ON query w/ ORDER BY max value of a column

落花浮王杯 提交于 2019-12-07 20:33:28
I've been tasked with converting a Rails app from MySQL to Postgres asap and ran into a small issue. The active record query: current_user.profile_visits.limit(6).order("created_at DESC").where("created_at > ? AND visitor_id <> ?", 2.months.ago, current_user.id).distinct Produces the SQL: SELECT visitor_id, MAX(created_at) as created_at, distinct on (visitor_id) * FROM "profile_visits" WHERE "profile_visits"."social_user_id" = 21 AND (created_at > '2015-02-01 17:17:01.826897' AND visitor_id <> 21) ORDER BY created_at DESC, id DESC LIMIT 6 I'm pretty confident when working with MySQL but I'm

How to use 2 different database adapters (SQL Server and PostgreSQL) in Rails in the same environment?

末鹿安然 提交于 2019-12-07 20:16:45
问题 I'm working on a Rails 3 project which uses a PostgreSQL database. I need to make queries on a remote SQL Server database sometimes (pure sql and stored procedures) To do the trick I can use TinyTDS to access the SQL Server and execute queries: development: adapter: sqlserver mode: dblib host: 123.123.123.123 port: 1433 database: the_db_name username: the_user password: the_pwd timeout: 5000 It's working well, but I don't know how to use the 2 different database adapters at the same time. Is

Rails 4 MySQL bigInt primary key issues and errors

旧街凉风 提交于 2019-12-07 20:10:50
问题 I need to use a 14-digit bigInt as a primary key in a rails 4.1.8 application. Using older posts on SO as a guide, I came up with the following to address this... class CreateAcctTransactions < ActiveRecord::Migration def change create_table "acct_transactions", :id => false do |t| t.integer :id, :limit => 8,null: false t.integer "account_id",limit: 8,null: false t.integer "transaction_type_id", null: false t.datetime "date",null: false t.text "description",limit: 255 t.decimal "amount"

STI Inheiritance in Rails. Issues with find

匆匆过客 提交于 2019-12-07 19:25:12
问题 I am having an issue with searching for records in my STI table due to my inheritance structure class User < ActiveRecord::Base class LegacyUser < User class AuthUser < User class SuperUser < AuthUser class FieldUser < AuthUser class ClientAdmin < AuthUser The problem is that find does not work for the AuthUser Model. The query is looking for type "AuthUser" and does not include the other three possibilities. Edit: While playing around with this it started to work but only for ClientAdmin and

How Activerecord polymorphic mechanism can be used for MTI implementation?

空扰寡人 提交于 2019-12-07 18:48:32
问题 I have found this Q-A Why does ActiveRecord in Rails not support Multiple Table Inheritance? And now i have a question according to CdotStrifeVII's answer Polymorphic associations fill that role for most use cases do you agree with this? As for me, polymorphic is more like STI implementation... How rails polymorphic mechanism can be used to implement MTI? 来源: https://stackoverflow.com/questions/40073707/how-activerecord-polymorphic-mechanism-can-be-used-for-mti-implementation

Codeigniter active record update with join

时光毁灭记忆、已成空白 提交于 2019-12-07 18:47:56
问题 i have two tables: table1: id, user_id, poll_id, options_id table2: id, poll_id, votes column votes is an integer and i want to change the value by joining the tables with some where clauses: $this->db ->set('votes', 'votes - 1', FALSE) ->join('table1', 'poll_votes.options_id = table2.id') ->where('poll_id', $row) ->where('user_id', $id) ->update('table2'); I´m getting this error: Error Number: 1054 Unknown column 'user_id' in 'where clause' UPDATE `table2` SET votes = votes - 1 WHERE `poll

Upgrading to Rails 3.2.2: How to solve the 'undefined method for Syck::DomainType' error related to the Delayed Job gem?

孤街浪徒 提交于 2019-12-07 18:05:04
问题 I am running Ruby on Rails 3.2.2 from 3.1.0. I have the issue undefined method send_register_email\' for #<Syck::DomainType:0x0000012d2346b8>\n/<ABSOLUTE_PATH>/.rvm/gems/ruby-1.9.2-p290/gems/delayed_job-3.0.1/lib/delayed/performable_mailer.rb:6:in perform ... with the DelayedJob gem that many other people have tried to solve: someone made successfully that, others no. I am in the latter category, also if I tried all solutions I've found on the Web. At this time, in my Gemfile I have: gem

Does ActiveRecord find_all_by_X preserve order? If not, what should be used instead?

别说谁变了你拦得住时间么 提交于 2019-12-07 17:26:54
问题 Suppose I have a non-empty array ids of Thing object ids and I want to find the corresponding objects using things = Thing.find_all_by_id(ids) . My impression is that things will not necessarily have an ordering analogous to that of ids . Is my impression correct? If so, what can I used instead of find_all_by_id that preserves order and doesn't hit the database unnecessarily many times? 回答1: Yes Use Array#sort Check it out: Thing.where(:id => ids).sort! {|a, b| ids.index(a.id) <=> ids.index(b