activerecord

How to order results by an existing boolean attribute first

情到浓时终转凉″ 提交于 2019-12-11 09:45:56
问题 I have many records with a featured? attribute. I would like to query all, group them in featured and non-featured and if possible random them in between their groups (random and show the featured ones, then random the rest). Any idea how to do this on ActiveRecord? 回答1: Use rand() MySQL SELECT * FROM <TABLE_NAME> ORDER BY featured?, rand() RAILS 3 ModelName.order("featured, rand()") For Ex:- id featured 1 true 2 false 3 false 4 true 5 false 6 true i want all the featured with value true

NameError: uninitialized constant for has_many through relationship

和自甴很熟 提交于 2019-12-11 09:42:20
问题 I can't seem to get this right. My has many through relationship just isn't working. Here's the setup: class Group < ActiveRecord::Base belongs_to :user has_many :groups_phone_numbers, :dependent => :destroy has_many :phone_numbers, through: :groups_phone_numbers attr_accessible :name end class PhoneNumber < ActiveRecord::Base belongs_to :user has_many :responses has_many :groups_phone_numbers has_many :groups, through: :groups_phone_numbers attr_accessible :label, :number end class

Rails: Chaining filter methods on a model

99封情书 提交于 2019-12-11 09:37:57
问题 My web app is showing a list of articles. Articles can be published in different countries (i.e. Article belongs to a Country, and Country has many Articles), and in different languages (i.e. Article belongs to a Language, and Language has many Articles). What I would like to do is to make requests that will return me articles from selected countries and in selected languages. That is, I would eventually like to make a request such as this: Article.selected_countries.selected_languages and

Rails count records without querying again

拜拜、爱过 提交于 2019-12-11 09:35:26
问题 Say i queried table rawStats = Stats::TrackProcessed.select("token") and received number of all tokens in a table. Looks like this: 3e79a387c29bda1069271e06ad03d82b8296242e 059681f46ab1c1fa8cf8443a82f0898172e0b646 eacd846ea4e91b49f92f416f61e0f2d075b9dae7 eacd846ea4e91b49f92f416f61e0f2d075b9dae7 811705019a970929801adbf3db0ede31ed01816c I need to return a hash list that will look like this { '3e79a387c29bda1069271e06ad03d82b8296242e' => 1, '059681f46ab1c1fa8cf8443a82f0898172e0b646' => 1

Setting default value for a json column

女生的网名这么多〃 提交于 2019-12-11 09:35:09
问题 I am looking at using Postgres's feature of setting json into a column via activerecords json handling features I am wondering how I would give it a default value upon table creation of something like {name: '', other_name: ''} and so on ... I am also looking to understand how if I create a default json value for a column, like the example above and then later on I fill in the values, but then at a some other time reset it back to "default" how that would look. 回答1: It's just like any other

Ruby on Rails Join Table Associations

守給你的承諾、 提交于 2019-12-11 09:33:42
问题 I have a Ruby on Rails application setup like so: User Model has_and_belongs_to_many :roles Role Model has_many :transactions has_and_belongs_to_many :users Transaction Model belongs_to :role This means that a join table is used called roles_users and it also means that a user can only see the transactions that have been assigned to them through roles, usage example: user = User.find(1) transactions = user.roles.first.transactions This will return the transactions which are associated with

sql query order problem

别来无恙 提交于 2019-12-11 09:16:46
问题 So I've got the follow model in activerecord, I will use Discussion.user_replied_group_discussions(@user) it returns the recent replied discussions alright, but how do I modify the private method to order the returned discussions by latest reply time? # Table name: discussions # # id :integer not null, primary key # title :string(255) not null # content :text(255) not null # created_at :datetime # updated_at :datetime # user_id :integer # discussable_id :integer # discussable_type :string(255

Rails ActiveRecord: how to get non-duplicate child records by super-parent in view?

橙三吉。 提交于 2019-12-11 09:07:04
问题 I have four tables called Albums , Songs , Singers and SongSingers . Albums { Id, Name } Songs { Id, AlbumId, Name } Singers { Id, Name } SongSingers { Id, SongId, SingerId } Album has many songs. Song belongs to an Album. SongSinger belongs to Song and Singer. Song and Singer have many SongSingers. Each song may have same or different singers. In View, how can I get all non-duplicate Singers by AlbumId. Thanks. 回答1: Absolutely no problem. You are querying for singers. That's what the query

ActiveRecord joins on aggregate query

余生长醉 提交于 2019-12-11 08:56:47
问题 I have a Unit model that has_many :transacions . Transaction has a type column that indicates what kind fo transaction it is: sale, transfer, reservation, etc. I would like to find all units that have a given transaction type as their latest transaction. I can't figure out how to do it in ActiveRecord. In SQL, if for example I wanted to find all units with "Transfer" as their latest transaction I could do: SELECT u.*, tx.* FROM units u INNER JOIN transactions tx ON u.id=tx.unit_id INNER JOIN

How to delete all records in a table using SubSonic 3

点点圈 提交于 2019-12-11 08:41:01
问题 I'm trying to delete all the records from a table using this approach: new Delete<Contact>().Execute(); This statement fails with a NullReferenceException in BuildDeleteStatement method at line: sb.Append(query.FromTables[0].QualifiedName); Because, although FromTables has one entry, it is set to null. I also tried this but it doesn't worked either: var provider = ProviderFactory.GetProvider("MonitorData"); new Delete<Contact>(provider).Execute(); What am I doing wrong? 回答1: You can do this