activerecord

Remove Element from an ActiveRecord-hash without delete on DB

守給你的承諾、 提交于 2019-12-13 03:39:11
问题 how can i do this? all_tickets = Ticket.find(:all) all_tickets.each do |at| if at "a condition" all_tickets.delete(at) end end With this code i delete the at-element from database, but i only want to remove the at-element from the all_tickets ActiveRecord-Hash when the "a condition" is true. Thanks for Help 回答1: This solution has been converted to NOT use sql per your hint. In short, grab the tickets, loop over them and if the condition is true, delete the particular key from the attributes.

ActiveModel::UnknownAttributeError: unknown attribute when seeding belongs_to relationship

守給你的承諾、 提交于 2019-12-13 03:32:19
问题 Trying to seed a has_many/belongs_to relationship. A user has_many scripts, a script belongs_to a user. When seeding the script, I get this error: ActiveModel::UnknownAttributeError: unknown attribute 'user_id' for Script. I'd think that this line in my migration would create a user_id attribute for script? t.belongs_to :user, index: true, foreign_key: true app/models/script.rb class Script < ApplicationRecord belongs_to :user has_many :commits has_many :script_users, through: :commits,

How to make a Rails model read-only except for specified attributes?

╄→尐↘猪︶ㄣ 提交于 2019-12-13 03:27:01
问题 I have a rails active record model Application and need to make it read-only. I know that having a before_validation method with: if locked def readonly? true end end Will allow me to make it readonly. The only issue is that, when I need to disable readonly mode, I can not change the locked value to false because the model is already read-only. I know that attr_readonly :name, :email, :price etc. will allow me to make specific attributes readonly, I am looking for the inverse of this to only

Connect rails to existing postgres DB - models not seen by console and controllers

若如初见. 提交于 2019-12-13 03:23:24
问题 I have a database with several tables, each one containing columns that may not follow the rails naming convention. Is there a tool existing to create the ActiveRecord models from those tables or do I need to do this at hand, one by one ? If I create the ActiveRecord model for one table by hand, would this be ok though ? (no hidden DB identifier needed on top of it ?) UPDATE I have tried magicmodels but cannot have it working (it has been a while since it was last modified) and does not seem

How to find duplicate records in ActiveRecord other than original one

∥☆過路亽.° 提交于 2019-12-13 03:17:42
问题 Using Rails 4, Ruby 2, MySql I would like to find all the records in my database which are repeats of another record - but not the original record itself. This is so I can update_attributes(:duplicate => true) on each of these records and leave the original one not marked as a duplicate. You could say that I am looking for the opposite of Uniq * I don't want the Uniq values, I want all the values which are not uniq after the fact. I don't want all values which have a duplicate as that would

Inserting values in postgres but they are interpreted as column

匆匆过客 提交于 2019-12-13 03:13:45
问题 I am trying to insert some data in my postgres databae like this: def insert_row(conn, row) attendee = map_row_to_struct(row) conn.execute( <<-SQL INSERT INTO tmp_attendee_import (email, first_name, last_name) VALUES("#{attendee.email}", "#{attendee.first_name}", "#{attendee.last_name}"); SQL ) end The SQL is evaluated wonderfully as: INSERT INTO tmp_attendee_import (email, first_name, last_name) VALUES("myemail@yahoo.com", "Gigel", "Ion"); Yet I get this error: Failure/Error: conn.execute( <

display posts by month/year ruby

喜你入骨 提交于 2019-12-13 03:11:52
问题 Not sure where to start with this so here goes.. I am building a small blog in which the date of each post is displayed, overtime there will be many blog posts per month, for which i would like to group together by the month it was published. I want to display it like this in the view Archives January 2013 February 2013 March 2013 etc When i click on a given month the idea is it will take me to all the posts that where published within that month. So far I can group all the posts by month and

SubSonic3 VB.Net Add,Update, FirstOrDefault Problems

冷暖自知 提交于 2019-12-13 03:07:11
问题 I hade build sctive record dal with subsonic3 Vb.net templates. and i am dealing with alot of bugs in the sub sonic dlls. 1)in Add() function: (i have fix) when indx has counter in the db the returnd new key type is decimal the active record fil have an exception "Public member 'Change Type To' n type 'Decimal' not found". i managed to fix this bug. i changed in the activeRecord template the sub OlD Public Sub SetKeyValue(value As Object) Implements IActiveRecord.SetKeyValue If value IsNot

subsonic, mvc and activerecord

雨燕双飞 提交于 2019-12-13 02:56:21
问题 i am using subsonic 3.0 and active record with a mysql database now everything is fine, but i cant seem to create views see example: public ActionResult Index() { return View(contact.GetPaged(1,20)); } now normally i would right click and choose Add View i would then choose strongly typed and find the class for the repositary however for some reason the only classes i get showing up are for subsonic only but expect to see the new class from the new generated graniteMysqlDB if anyone could

Rails activeadmin/carrierwave has_many photos relationship throwing mass-assignment exception

China☆狼群 提交于 2019-12-13 02:56:14
问题 I have a model BlogPost defined as class BlogPost < ActiveRecord::Base attr_accessible :title, :body, :photo, :photos_attributes, as: :admin has_many :photos, class_name: 'BlogPhoto', dependent: :destroy validates :body, :photo, :title, presence: true validates :title, uniqueness: true accepts_nested_attributes_for :photos, allow_destroy: true end from schema create_table "blog_posts", :force => true do |t| t.string "title" t.text "body" t.datetime "created_at", :null => false t.string "photo