activerecord

dynamic table names for Active Record models

*爱你&永不变心* 提交于 2019-12-31 07:16:38
问题 I have an interesting Active Record problem and I'm not quite sure what the cleanest solution is. The legacy database that I am integrating with has a strange wrinkle in its schema where one logical table has been 'partitioned' into several physical tables. Each table has the same structure, but contains data about different items. I'm not great at explaining this clearly (as you can tell!). Let me try and explain with a concrete example. Let's say we have a Car, which has one or more Wheels.

Invalid source reflection macro :has_many :through

点点圈 提交于 2019-12-31 03:14:41
问题 I have such angry associations: financings >- events >- subprograms >- programs. I want to get acces to last_financings from programs through all of them so code is: class Fcp < Program has_many :fcp_subprograms, :foreign_key => 'parent_id' has_many :subprogram_last_actual_financings, :through => :fcp_subprograms, :source => :last_actual_financings class FcpSubprogram < Program belongs_to :fcp, :class_name => 'Fcp', :foreign_key => 'parent_id' has_many :events, :foreign_key => 'fcp_id' has

HABTM relation find all records, excluding some based on association

女生的网名这么多〃 提交于 2019-12-31 03:05:17
问题 I've looked at some of the similar SO posts relating to this but I'm struggling to get my head around it. I have a habtm relation between Projects and Users. I'm trying to find all the Projects that a particular user does not belong to but I don't know how. I've tried this sort of thing: Project.where('project_id != ?', user.id) But it's also obviously wrong. I'm using rails 3.2.x Many of the answers relating to this mention scopes but I haven't come across them before (I'm still very new to

How to find distinct years from a table with multiple years in Rails

旧时模样 提交于 2019-12-31 01:58:07
问题 I have a table with several hundred records, each one containing a datetime column. What is the most efficient way to find the distinct years from that datetime field? 回答1: Using Ruby (this would require loading all the records, though): Model.select("my_datetime").map{ |item| item.my_datetime.year }.uniq Using SQL (this example is for PostgreSQL, I can update it with your specific database): # PostgreSQL Model.select("distinct(extract(year from my_datetime))") # MySQL Model.select("distinct

Active Record where join table record doesn't exist

大憨熊 提交于 2019-12-30 14:10:34
问题 I am trying to get a list of all records that don't exist in a join table. The models are User, Game and MarkedGame, where users can mark games as played. It's a many to many relationship: User > MarkedGame < Game What I want is a list of all games that haven't been marked by the user. I know that I could do two separate queries and subtract them: Game.all - current_user.games But I don't like that this leaves me with an array rather than an Active Record relation object. Plus it seems like

newbie: append serialized integers into database column and retrieve them back

安稳与你 提交于 2019-12-30 11:53:41
问题 How Could I store integers (user id's ranging from 1 to 9999) serialized in a database column and retrieve them back? In my User model I have invites column, User model serialize: invites invites = text field Now I'm trying to do 2 things: Append the user_id integer (from 1 to 9999) serialized in a column "invites" Retrieve all the user id's back from the User.invited column ( deserialize it ? ) 回答1: From the fine manual: serialize(attr_name, class_name = Object) If you have an attribute that

Getting count of elements by `created_at` by day in a given month

旧城冷巷雨未停 提交于 2019-12-30 09:58:30
问题 I wanted to make a simple chart of users that have been created in the past month in my app. Like basically for each day in the past month I want to show the count of users that have registered that day. What I have so far: # Controller @users = User.count(:order => 'DATE(created_at) DESC', :group => ["DATE(created_at)"]) # View <% @users.each do |user| %> <%= user[0] %><br /> <%= user[1] %> <% end %> # Output 2010-01-10 2 2010-01-08 11 2010-01-07 23 2010-01-02 4 Which is ok, but if no users

How can I override the attribute assignment in an active record object?

∥☆過路亽.° 提交于 2019-12-30 09:48:01
问题 I know you can do this with virtual attributes, but what if the column actually exists? For example, my model has a raw_topic column. When raw_topic is set, I want artist and song_title to be set based off of raw_topic 's contents. Ideally, I'd like to override the raw_topic= method, but rails doesn't seem to like that. What's the proper way of doing this? Is a callback the only way? 回答1: You can do it like this: def raw_topic=(value) # do something with raw topic self[:raw_topic] = value end

Group Users by Age Range in ruby

三世轮回 提交于 2019-12-30 09:32:32
问题 I'm trying to list the number of users by age-range: Range : #Users 10-14 : 16 15-21 : 120 22-29 : 312 30-40 : 12131 41-70 : 612 71-120 : 20 I was thinking of creating a static array of hashes: AGE_RANGES = [ {label:"10 - 14", min:10, max:14}, {label:"15 - 21", min:15, max:21}, {label:"22 - 29", min:22, max:29}, {label:"30 - 40", min:30, max:40}, {label:"41 - 70", min:41, max:70}, {label:"71 - 120", min:71, max:120} ] and then use it for my search filter, as well as for my query. But, I

Losing an Attribute When Saving Through an Association w/ Scope (Rails 4.0.0)

霸气de小男生 提交于 2019-12-30 08:37:12
问题 The Code (Rails 4.0.0) class Track < ActiveRecord::Base has_many :artist_tracks has_many :owning_artists, -> { where(:artist_tracks => { :artistic_role_id => 1 }) }, :through => :artist_tracks, :source => :artist end class ArtistTrack < ActiveRecord::Base belongs_to :artist belongs_to :track belongs_to :artistic_role end class Artist < ActiveRecord::Base has_many :artist_tracks has_many :tracks, :through => :artist_tracks end Finding Works # artist_tracks.artistic_role_id is properly set to