activerecord

Is there a DELETED table in Rails just as in SQL Server?

假装没事ソ 提交于 2019-12-11 19:53:25
问题 I need to be able to do the same as demonstrated below in my rails app. As you can see, in SQL server there is a table called DELETED that stores those rows that were just deleted. source: How to create a before delete trigger in SQL Server? CREATE TRIGGER TRG_AUD_DEL ON yourTable FOR DELETE AS INSERT INTO my_audit_table (col1, col2, ...) SELECT col1, col2... FROM DELETED Applying this logic to rails, is there such a table/location to call from to get those rows just as they are deleted in

Elegantly set join table attributes on has_many through: association

泪湿孤枕 提交于 2019-12-11 19:44:29
问题 In my Rails 3.2 app Users can create Collections of Articles . Collections and Articles are joined by a has_many through: collections_articles relationship. I wish to store the user_id in the collections_articles join table when an Article is added to a Collection . (So the app can later show who added an article to a collection). Is there an elegant way to do this? class CollectionsArticle collection_id article_id user_id belongs_to :collection belongs_to :article belongs_to :user When a

Rails 4 Generating Invalid Column Names

一个人想着一个人 提交于 2019-12-11 19:43:06
问题 I have a fairly simple query to return the first record in a many-to-many relation or create one if it doesn't exist. UserCategorization.where(category_id: 3, user_id: 5).first_or_create My model looks like: class UserCategorization < ActiveRecord::Base belongs_to :user belongs_to :category self.primary_key = [:user_id, :category_id] end However it generates an invalid column name in the SQL: SQLite3::SQLException: no such column: user_categorizations.[:user_id, :category_id]: SELECT "user

Cannot assign ActiveRecord Attributes in Model

南楼画角 提交于 2019-12-11 19:29:32
问题 I am trying to assign values of ActiveRecord attributes within my models, but for whatever reason, I can't set them. For instance, I have an AccountModel and this has an Attribute name If I set it from the controller or the console (like user.name = "John" ), everything works fine. But, if I try to set it from within the model, like def set_name(new_name) name = new_name end then it doesn't work. On the other hand, retrieving the name, like def get_name name end works just fine. Am I missing

ActiveRecord belongs_to association does not save foreign_key (Rails 4)

感情迁移 提交于 2019-12-11 19:24:39
问题 This weekend I decided to take Rails 4 for a spin and promptly ran into the following issue: I have two models (wanted to try an OpenSchema incase you are wondering): Record has_many :ns_attributes NsAttribute belongs_to :record Now in the console: record = Record.create!(name: "blowing in the wind") nsa = NsAttribute.new(key: "artist", value: "bob dylan", record: record) #<NsAttribute id: nil, key: "artist", value: "bob dylan", record_id: 4, created_at: nil, updated_at: nil> irb(main):007:0>

return a row of a table using Active Record in Yii

邮差的信 提交于 2019-12-11 18:48:01
问题 I want to return a field of a row of table which has id = 4 in a model for example Post. Which one should I use find() method or findByAttributes() ? And what is the correct syntax for it ? 回答1: To grab a model by its primary key I'd suggest `findByPk(). It's one of the most simple methods to use; $id = 4; $model = Post::model()->findByPk($id); For other method syntaxes, have a read through the Yii Active Record wiki and Yii Active Record documentation, they're really helpful when starting

Active Record query to find parents with child within a date range and child prior to that date range

喜夏-厌秋 提交于 2019-12-11 18:47:32
问题 I need an active record query that returns the count of Parents with a Child created within a date range AND a Child created prior to the date range. I currently have the following two queries: This query returns the count of Parents with a Child created within the date range Parent.joins(:children).where("children.created_at BETWEEN ? AND ?", start, end).distinct.count(parent_id) This query counts the Parents with a Child created before the date range Parent.joins(:children).where("children

Cannot map Employee back to User ID correctly

隐身守侯 提交于 2019-12-11 18:37:05
问题 My problem is: I have a User model , Employee , Student , Parent . I don't know how to map each of Employee , Student , Parent back to User ID correctly. because in the current state for example I have differentiating by a normal user and an employee (since the employee is a user as well ) , How can I do so ? For example I will paste Employee and User and Ticket so you can help me to map it correctly because in my system Ticket model is not dealing with the Employee as a User . please I am

Has_many through :association Not Registering in Rails 5.2

有些话、适合烂在心里 提交于 2019-12-11 18:36:08
问题 So as of this question I had a has_and_belongs_to_many working with a join table between people and occasions . I had it working (with help from the lovely SO wizard that helped me), but said wizard recommended that a has_many through: :association might work better for my purposes. Unfortunately, it definitely does, but in shifting over I seem to have broken it. My schema.rb now reads like this, using gifts to connect people and occasions : create_table "gifts", force: :cascade do |t| t

has_many and sum named_scope

与世无争的帅哥 提交于 2019-12-11 18:33:43
问题 I have this situation: Stories has many Tasks Tasks have an integer called hours_left I need a named scope to find Stories which all its tasks has more than 0 hours left . Based on this post. I wrote this: class Story has_many :tasks named_scope :uncompleted, { :joins=>["INNER JOIN tasks ON tasks.story_id = stories.id"], :group=> 'stories.id', :select=>'stories.*, SUM(tasks.hours_left) AS sum_amount', :having=>"sum_amount > 0" } end But Story.uncompleted returns an empty array. Can you help