activerecord

Manually instantiate ActiveRecord models and their relationships?

随声附和 提交于 2019-12-10 19:07:02
问题 If I have T-SQL (or a stored proc) that returns records from multiple tables (using DBI perhaps), is there a way for me to manually instantiate the ActiveRecord models and their associations? Obviously, I’m after database performance here. I would like to be able to build my own object hierarchy (models and their relationships), but when I’m all done I would expect each model to behave normally. That is, I am hoping this would be accomplished without some hack that might cause my structure to

Rails reset single column

自作多情 提交于 2019-12-10 18:55:49
问题 I am trying to implement a rake task that will run once a month to reset a single column. I would prefer to reset the column to its default value but I cannot find any methods to help me accomplish this. reset_column_information does not work What is the most efficient way to reset a single column in active record? 回答1: Base method #update_all does the update direct in the database, so it is very efficient. However it bypasses callbacks because the models aren't loaded: http://apidock.com

Rails 2.3.5 Problem Building Conditions Array dynamically when using in (?)

不问归期 提交于 2019-12-10 18:51:30
问题 Rails 2.3.5 I've looked at a number of other questions relating to building conditions dynamically for an ActiveRecord find. I'm aware there are some great gems out there like search logic and that this is better in Rails3. However, I'm using geokit for geospacial search and I'm trying to build just a standard conditions set that will allow me to combine a slew of different filters. I have 12 different filters that I'm trying to combine dynamically for an advanced search. I need to be able to

Problems with infinite time range in Rails

一世执手 提交于 2019-12-10 18:43:57
问题 On a table with both time range (Postgres type tsrange ) and date range (Postgres type daterange ) fields... Saving and then retrieving an unbound/infinite time range from DB causes an error: object.a_time_range = Time.now.utc..DateTime::Infinity.new object.save! object.reload => ArgumentError: bad value for range However, doing the same with date range works fine: object.a_date_range = Date.today..Date::Infinity.new object.save! object.reload Is there a way to use infinite time ranges in

Scope on each last elements of a has_many relation

耗尽温柔 提交于 2019-12-10 18:35:12
问题 Let's say I have a has_many relation between User and Messages. I'd like to set a scope to be able to filter users by the ones who have something in the last message they posted. So searching only among each user's last message. Below I got the results among all messages... class Contact < ActiveRecord::Base has_many :messages scope :filter_by_last_messages, lambda { |value| joins(:messages).where("messages.content = ?", value) } end 回答1: Doing this in one shot in a scope is not possible but

What is the difference with find_by() from the core and the one from the FinderMethods?

China☆狼群 提交于 2019-12-10 18:21:08
问题 Currently I'm working on a gem, which overrides ActiveRecords where . By working on that, I stumbled on two different find_by implementations. One is in the core and it uses some kind of cache, whereas the one from the FinderMethods module calls where directly. What is the difference between these two implementations? When is which used? 回答1: I think it's that way: When you use something like this: User.find_by(...) The ActiveRecord::Core#find_by is called, as the Core is included into Base

has_one :through => multiple

走远了吗. 提交于 2019-12-10 18:17:26
问题 Both Attendment & Vouching: belongs_to :event belongs_to :account Therefore: 1 to 1 relationship between attendments and vouchings. Is there a way to do this without my thinking too much? # attendment has_one :vouching :through => [:event, :account] Note: I don't mind thinking too much, actually. 回答1: Yeah i don't think you can use a has_one for this. Assuming I'm reading this correctly, you have two models: Attendment Vouching They both store an event_id and account_id. You want to know from

How to get a scope for all database rows in rails 3?

≡放荡痞女 提交于 2019-12-10 18:16:35
问题 assume we the following setup: class Post < ActiveRecord::Base belongs_to :user end class User < ActiveRecord::Base has_many :posts end assume further the user has a boolean attribute 'admin', which indicates if he is a global admin or not. I want to write a method (or a scope?) for the User class, called 'visible_posts'. If the user is no admin, it should return just its own posts. If he IS admin the method should return all posts in the system. My first attempt was something like this:

Remove association model in Rails

爷,独闯天下 提交于 2019-12-10 18:13:37
问题 Hi i would like to know if its possilbe to remove an association in rails. Well, i have something similar to this: class Home < ActiveRecord::Base include settings end On settings.rb i have something similar to this module Settings attr_reader :person attr_reader :address def self.included(base) base.belongs_to :city base.belongs_to :entity [...] end [...] end On Home class the city model association on my particular case don't make sense. And i have to find a way to remove it to maintain my

Yii: Select 20 last entries order by id ASC

荒凉一梦 提交于 2019-12-10 18:01:03
问题 I would like to get the 20 last entries of my table but order by ascending id. In Sql it's not very complicated: SELECT * FROM (SELECT * FROM comments WHERE postID='$id' ORDER BY id DESC LIMIT 20) t ORDER BY id ASC; But I would like to to it with my yii model like: Comment::model()->findAll($criteria) But i really don't know what I should put in my CDbCriteria! 回答1: There is a way without using array_reverse , if you think of using this sql: SELECT * FROM `comments` `t` WHERE id in (SELECT id