activerecord

rails 3.1 active record insert or update

与世无争的帅哥 提交于 2019-12-23 19:08:19
问题 I'm new to rails. Is there an easy way in active record to pass it a hash of data and if the record exists, update it, and if it doesn't, create it? data = {} data["my_id"] = 356345 data["description"] = "test123" w = Descriptions.new(data) Ideally if I ran the above it would only ever have 1 record, not multiple records each time I ran it. 回答1: Assuming you ware wanting the "my_id" bit to be unique you can run Descriptions.find_or_create_by_my_id(data["my_id"]).update_attributes(data) 回答2:

Use SQL functions for insert/update in ActiveRecord

浪尽此生 提交于 2019-12-23 17:38:13
问题 I want to store IP addresses (v4 and v6) in my rails application. I have installed an extension to MySQL adds functions to convert ip strings to binary which will allow me to query by IP range easily. I can use unescaped sql statements for SELECT type queries, thats easy. The hard part is that I also need a way to overwrite the way the field is escaped for insert/update statements. This ActiveRecord statement new_ip = Ip.new new_ip.start = '1.2.3.4' new_ip.save Should generate the following

Convert from query to ModelSearch of Yii2

北战南征 提交于 2019-12-23 17:18:47
问题 I'm new in Yii2, and I have a query with right result: SELECT DISTINCT workloadTeam.project_id, wp.project_name, workloadTeam.user_id, workloadTeam.commit_time, wp.workload_type FROM (SELECT p.id, p.project_name, w.user_id, w.commit_time, w.comment, w.workload_type FROM workload as w, project as p WHERE w.user_id = 23 AND p.id = w.project_id) wp INNER JOIN workload as workloadTeam ON wp.id = workloadTeam.project_id But in my ModelSearch.php, I wrote: $user_id = Yii::$app->user->id; $subquery

Rails: Belongs_to Polymorphic Association + conditions

坚强是说给别人听的谎言 提交于 2019-12-23 17:10:04
问题 So I have this model: class Model < ActiveRecord::Base attr_accessible :to_id, :to_type belongs_to :to, polymorphic: true end I was wondering if I could add another relationship when belongs_to is on a specific type: class Model < ActiveRecord::Base attr_accessible :to_id, :to_type belongs_to :to, polymorphic: true belongs_to :to_user, :foreign_key => :to_id, :conditions => ['to_type = ?', 'User'] # doesn't work # OR MAYBE belongs_to :to_user, :foreign_key => :to_id, :foreign_class => 'User'

Is there a way to remove the id column in a subsequent Rails 3 ActiveRecord migration?

早过忘川 提交于 2019-12-23 17:00:41
问题 In Rails 3 ActiveRecord create_table is it possible to include the option :id => false. For example create_table :posts, :id => false do |t| ... end but is it possible to remove the :id column on an existing table in a subsequent up migration? 回答1: You should be able to remove the column just like any other non-id column: remove_column :posts, :id 来源: https://stackoverflow.com/questions/8394253/is-there-a-way-to-remove-the-id-column-in-a-subsequent-rails-3-activerecord-migr

Mass-assign exception no explanation found (Rails 3.2.1)

依然范特西╮ 提交于 2019-12-23 17:00:39
问题 Please see this post as well. Note: The current rake task saves User and Topic objects, but not posts or tags when setting a post object. Question: What is a proper way to describe this relationship in my rake task (shown below) so that it does not throw a can't mass assign attributes exception? Here were my steps: Step 1: Create models class Post < ActiveRecord::Base belongs_to :topic, inverse_of: :posts has_and_belongs_to_many :tags attr_accessible :title, :description, :content, :tags

Database configuration does not specify adapter (Sinatra + Heroku + Activerecord)

坚强是说给别人听的谎言 提交于 2019-12-23 16:54:49
问题 I'm getting an error (on Sinatra + ActiveRecord Heroku) that Database configuration does not specify adapter . From some research, it looks like this is because Heroku expects there to be no environmental variables used during rackup. I removed the env vars from the db environments file that listed the database URL, but I'm still getting the same error. EDIT: This also worked just a day ago with the db files unchanged, so I'm not sure what could be wrong. 2015-09-01T02:44:40.980448+00:00 app

Multi-step ActiveRecord's Model validation

我的未来我决定 提交于 2019-12-23 16:26:52
问题 Consider a User model with the following fields: First name (required) Last name (required) Email (required) Password (required) Phone (required, size: 10 digits) Address (required) And a multi-step signup form with the following steps: 1st step with fields First Name, Last Name, and Email 2nd step with Password, Phone and Address. How would you create a solution to validate the input in each step? The standard ActiveRecord's way doesn't works, since it validates all fields at once. I've

Maximum in group by active record query

二次信任 提交于 2019-12-23 15:44:00
问题 I have a Model which has two attributes (I am showing only two as only those two columns are needed) MyModel place_id ------------------------ user_id 1 ---------------------------------- 1 1 ---------------------------------- 2 1 ---------------------------------- 2 3 ---------------------------------- 3 3 ---------------------------------- 2 3 ---------------------------------- 3 3 ---------------------------------- 1 I want to fetch group by maximum records. Basically I want to fetch for a

Changing the default ActiveRecord id from 0 to 1000000

吃可爱长大的小学妹 提交于 2019-12-23 14:25:23
问题 I'd like to change the minimum for the id of created objects from 1 to 1000. So when I create in rails my first model object it gets the ID 1000 and not 1. Is there a way to set this in the schema/migration files? 回答1: I am not familiar with MySQL, but for Postgres you can do something like this in your migration file: class CreateCustomers < ActiveRecord::Migration def self.up create_table :customers do |t| t.string :name t.timestamps end execute "SELECT setval('customers_id_seq', 1000);"