activerecord

CodeIgniter Active Record select with 2 or more tables

十年热恋 提交于 2019-12-25 04:52:26
问题 I have this sql statement setup in my old code and want to move it over to active record, the problem that I'm phased with is how do I do a SELECT news_items.id FROM (SELECT news_items.* FROM news_items JOIN feeds ON feeds.id=news_items.feed LEFT JOIN news_item_country ON news_item_country.id=news_items.country WHERE news_items.id=30758 ORDER BY date_item DESC ) AS news_items GROUP BY news_items.id ORDER BY date_item DESC Trying to do this with Active Record, first I created the select that

ActiveRecord not returning when searching on parent's children

≯℡__Kan透↙ 提交于 2019-12-25 04:45:27
问题 Would love to know what simple error it is that I'm making this time... current_user = User.find(60) Plan.joins(:user).where("users.id" => current_user.id).where("plans.status" => nil) # Plan Load (8.6ms) SELECT "plans".* FROM "plans" INNER JOIN "users" ON "users"."id" = "plans"."user_id" WHERE "users"."id" = 60 AND "plans"."status" IS NULL # => #<ActiveRecord::Relation [#<Plan id: 54....] current_user.plans.where("status == ?",nil) # Plan Load (0.2ms) SELECT "plans".* FROM "plans" WHERE

convert select and collect methods into joins

做~自己de王妃 提交于 2019-12-25 04:45:19
问题 I am learning joins I have three models class Booking < ActiveRecord::Base has_and_belongs_to_many :groups has_and_belongs_to_many :users end class User < ActiveRecord::Base belongs_to :group has_and_belongs_to_many :bookings end class Group < ActiveRecord::Base has_many :users has_and_belongs_to_many :bookings end booking = Booking.first booking.groups.collect{|g| g.users.select{|u| !self.users.include? u}}.flatten I want to get all users which are not booked in booking groups some thing

rails 3.1, how to calculate answers number using foreign key?

谁都会走 提交于 2019-12-25 04:44:11
问题 I have the following tables: class FinalExam < ActiveRecord::Base belongs_to :course has_many :pages, :as => :course_unit, :dependent => :destroy end class Page < ActiveRecord::Base belongs_to :course_unit, :polymorphic => true has_one :quiz has_one :slide end class Quiz < ActiveRecord::Base belongs_to :page end class Answers < ActiveRecord::Base belongs_to :quiz end So, we have Answer.Quiz.Page.FinalExam Given the FianlExam id, what is the fastest way to find all Answers count ? how to get

Selecting which value to have a unique id on model

喜你入骨 提交于 2019-12-25 04:41:33
问题 I have two models Deals and Restaurants I want to display the deals for each restaurant. I do not want them linked on :restaurant_id , rather i want them linked on :restaurant_name . What I cant figure out is how to link them on :restaurant_name in the controller restaurant controller def show @restaurant = Restaurant.find(params[:id]) @deals = @restaurant.deals end show.html.erb <% restaurant.deals.each do |deal| %> <h2><center><%= deal.day %></center></h2> <% end %> Any Ideas? 回答1: I want

In Zend_Auth, can I get a domain-model User object instead of stdClass?

喜欢而已 提交于 2019-12-25 04:35:10
问题 While working on the login part of an application, I find myself running into an issue: My model is based on Active Record, by extending Zend_Db_Table_Row objects. Whenever I have to deal with a user, I'd want to do this through the User object (being an extended table row). However, Zend_Auth_Adapter_DbTable::getResultRowObject() returns a stdClass and I can not find a way to tell Zend_Auth_Adapter_DbTable to use a specific Db_Table. My three options that I've found so far: Write a custom

Ruby on Rails, SQL Server order by and comparison

梦想的初衷 提交于 2019-12-25 03:59:12
问题 I am having trouble with the following queries. I am not sure why. This is the user model: class UserProfile < ActiveRecord::Base self.table_name = "UserProfile" self.primary_key = "UserId" has_many :user_access_job_list, class_name: 'UserAccessJobList', foreign_key: 'UserId' has_many :job_tables, class_name: 'JobTable', through: :user_access_job_list has_many :order_histories, class_name: 'OrderHist', through: :job_tables has_many :order_hist_lines, class_name: 'OrderHistLine', through: :job

ActiveRecord column decimal default value

耗尽温柔 提交于 2019-12-25 03:57:20
问题 I have a tableless class defined like this: class MyClass class MyClassRules < ActiveRecord::Base column :a, :integer column :b, :boolean column :c, :datetime column :d, :decimal, :precision => 10, :scale => 4 column :e, :string end end The object of type MyClassRules gets automatically built when I instantiate an object of type MyClass , and it is stored serialized in a single column in the my_class table called rules and accessible from my_obj.rules . So I can also make calls like my_obj

Getting a nested object collection trough Active Record

夙愿已清 提交于 2019-12-25 03:56:31
问题 I am trying to retrieve a list of object trough active record with no success I have a model which is: Store has many Products, Product has one Supplier class Store < ActiveRecord::Base has_many :products end class Product < ActiveRecord::Base belongs_to :supplier belongs_to :store end class Supplier < ActiveRecord::Base has_many :products end I am trying to get a list of suppliers from store trough product like this: self.products.supplier This gives me a undefined method exception 'supplier

Validation of encrypted data

给你一囗甜甜゛ 提交于 2019-12-25 03:55:37
问题 I encrypt all private data of users before storing in database with help of gem 'attr_encrypted'. For example, I have 'email_addresses' table, which contains 'encrypted_email' column. This gem decrypt data when I call object.email or when I search by emails. But I have issues with validation of this column. I have following validations for this and other tables: validates_length_of :email, :within => 3..100 validates_numericality_of :post_code I should decrypt data before validation somehow,