activerecord

Rails after_save callback to create an associated model based on column_changed?

匆匆过客 提交于 2019-12-12 08:04:28
问题 I have an ActiveRecord model with a status column. When the model is saved with a status change I need to write to a history file the change of status and who was responsible for the change. I was thinking an after_save callback would work great, but I can't use the status_changed? dynamic method to determine that the history write is necessary to execute. I don't want to write to the history if the model is saved but the status wasn't changed. My only thought on handling it right now is to

Rails ActiveRecord - Search on Multiple Attributes

一个人想着一个人 提交于 2019-12-12 07:57:57
问题 I'm implementing a simple search function that should check for a string in either the username, last_name and first_name. I've seen this ActiveRecord method on an old RailsCast: http://railscasts.com/episodes/37-simple-search-form find(:all, :conditions => ['name LIKE ?', "%#{search}%"]) But how do I make it so that it searches for the keyword in name, last_name and first name and returns the record if the one of the fields matched the term? I'm also wondering if the code on the RailsCast is

Codeigniter: does $this->db->last_query(); execute a query?

☆樱花仙子☆ 提交于 2019-12-12 07:38:58
问题 Does query execution happen at the get_where() clause of the following codeigniter active record statement? $this->db->select('*'); $q = $this->db->get_where('Contacts', array('id' => $contact_id)); $sql = $this->db->last_query(); Or does it happens once you call the result_array() ? And is $this->db->last_query(); a reliable way in getting the query string. 回答1: The query execution happens on all get methods like $this->db->get('table_name'); $this->db->get_where('table_name',$array); While

How to manage non-autoincrement primary key in Rails?

。_饼干妹妹 提交于 2019-12-12 07:38:36
问题 I have lots of situations where I'd like to have a non-autoincrement primary key when using Rails. Example: I have one-to-one relationship between A and B. B describes some specific features added to A thus can't exist without A. So we have: A has one B B belongs to A The natural thinking would be having B.A_id as primary key. So I tried create_table b, :id=>false in migration and set_primary_key :a_id in B's model, but it doesn't create actual primary keys in the database. And I want them

Codeigniter Active record: greater than statement

放肆的年华 提交于 2019-12-12 07:25:52
问题 I'm trying to convert a "greater than" where statement to CI's Active Record syntax. When I use this snippet $this->db->join('product_stocks', "product_stocks.size_id_fk = product_attributes.id", "left"); $this->db->where('product_stocks.stock_level', '> 1'); $result = $this->db->get('product_attributes')->result_array(); Then print $this->db->last_query(); shows WHERE product_stocks . stock_level = '> 1' which is of course not correct. Can this be done? 回答1: I think this should do the trick:

What's the best way to include a LIKE clause in a Rails query?

夙愿已清 提交于 2019-12-12 07:12:31
问题 What's the best way to include a LIKE clause in a Rails query i.e. something along the lines of (the completely incorrect): Question.where(:content => 'LIKE %farming%') 回答1: If this is Rails 3 you can use Arel's matches . This has the advantage of being database agnostic. For example: Question.where(Question.arel_table[:content].matches("%#{string}%")) This is somewhat clunky, but easily extracted to scopes, e.g.: class Question def self.match_scope_condition(col, query) arel_table[col]

In Rails, how do you assign all attributes of an item automatically depending on the first attribute

坚强是说给别人听的谎言 提交于 2019-12-12 06:36:56
问题 Basically, I want to generate an item, in this case, a ring. I have different TYPES of rings: class Ring < ActiveRecord::Base DISPLAY_NAMES [ 'Silver', 'Gold', 'Diamond' ] attr_accessible :description, :display_name, :roll, :bonus, :total, :image, :gold end Is there a way to randomize the DISPLAY_NAMES of rings? I'm guessing something like @ring.display_name = rand(Ring::DISPLAY_NAMES) ? Is there a better way to do this? Once that is found, is there a way to set all other attributes of the

Select record if the records has same id as any of the ID's in the Array [duplicate]

倖福魔咒の 提交于 2019-12-12 06:28:26
问题 This question already has an answer here : Selecting all Records if Record has any of the ID's from Array (1 answer) Closed 3 years ago . I am having trouble finding the right syntax and operator to check if a record has the same ID of any of the ID's stored in an Array. Is there a has any? function or similar? @problem = Remedy.where(["LOWER(\"remedyName\") LIKE?", "%#{params[:searchremedy]}%".downcase]).pluck(:id) @pretreatments = Treatment.joins(:remedies_treatment).where(:remedies

ERROR: missing FROM-clause entry for table “self” (Rails 4 Active Record transaction, postgresql 9.4))

南笙酒味 提交于 2019-12-12 06:19:28
问题 When creating a model Deal, I use an after_create to create prizes on the DealPrize table. Deal and DealPrize have a belong to/has_many relations: a Deal has many Deal prizes and a Dealprize belongs to a Deal. It works like this: inside Deal, I have a column 'prize-number' and I use an after_create so that evetytime the amdin creates a new deal, the app takes this prize_number column, and create this volume of prizes (inserting as many rows as necessary) inside the DealPrize table. I fail to

Rails 4 HABTM how to set multiple ids in console?

十年热恋 提交于 2019-12-12 06:13:06
问题 schema.rb: ActiveRecord::Schema.define(version: 20150324012404) do create_table "groups", force: :cascade do |t| t.string "title" t.integer "teacher_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false end create_table "groups_students", id: false, force: :cascade do |t| t.integer "group_id" t.integer "student_id" end add_index "groups_students", ["group_id"], name: "index_groups_students_on_group_id" add_index "groups_students", ["student_id"], name: "index_groups