activerecord

Union of 2 active record relation object in rails 3

你。 提交于 2019-12-11 18:25:10
问题 I have a content model represented by class: content . Now users can rate content , review content or do both. I want to find all the content that a user have either rated , reviewed or rated and reviewed . The reviews table has a many-to-one association with the content table (meaning a content can be reviewed many times). A similar relationship exists between the ratings table and the content table. I'm thinking I should do separate queries to find all rated content by a user, then all

How to load images/precompiled assets with PDFKit/wkhtmltopdf? Rails 5.2

孤者浪人 提交于 2019-12-11 18:22:37
问题 I'm having some success with creating PDFs with PDFKit, but the images are not loading from active_storage and assets resources. It fails every single time that I create the PDF, using show.pdf.erb. I'm using the default image_tag and assets are precompiled for production. How can I get PDFKit to find the precompiled assets folder for the images, and also find active_storage images that are supposed to load within the show.pdf.erb view? It works perfectly fine in the normal show.html.erb

CodeIgniter with Active Records and or_like

笑着哭i 提交于 2019-12-11 18:04:04
问题 I have an admin panel with a view for all the users info gotten from a Database table named "users". There's a search box and I use Active records to fetch the data. But there seems to be a problem with that and I can't figure it out. I'm pretty sure it's in the or_like part but the error confuses me. Here is the query I make passing one variable $data posting the info from the search box : public function get($data) { $this->db->select('id, email, firstname, lastname, usertype, ts_created,

undefined method `mysql_connection' for ActiveRecord::Base:Class

﹥>﹥吖頭↗ 提交于 2019-12-11 17:46:25
问题 I have upgrade rails2 to rails3 and when i try to rake db:migrate I have found following error `rake aborted!` undefined method `mysql_connection' for ActiveRecord::Base:Class /home/cis/.rvm/gems/ruby-1.9.3-p392@rails3214/gems/activerecord-3.2.14/lib/active_record/dynamic_matchers.rb:55:in `method_missing' /home/cis/.rvm/gems/ruby-1.9.3-p392@rails3214/gems/activerecord-3.2.14/lib/active_record/connection_adapters/abstract/connection_pool.rb:315:in `new_connection' /home/cis/.rvm/gems/ruby-1.9

How can I add DB level Unique constraint to a column in a table in Rails using the migration file?

我与影子孤独终老i 提交于 2019-12-11 17:41:23
问题 How can I add DB level unique constraint to a column in a table in Rails using its migration? I googled but those answer seems to involve index, and I don't want to touch index because I'm not sure if it will have bad side effect (because stackoverflow.com/a/3370333/6359753 has a comment saying it will have storage influence). Do I must have it and will it have bad side effect? This is why even though I have read A migration to add unique constraint to a combination of columns but still

how to save related models with non-null foreign key

为君一笑 提交于 2019-12-11 17:36:26
问题 I have two models: User has_one :email Email belongs_to :user I put the email_id foreign key (NOT NULL) inside users table. Now I'm trying to save it in the following way: @email = Email.new(params[:email]) @email.user = User.new(params[:user]) @email.save This raises a db exception, because the foreign key constraint is not met (NULL is inserted into email_id). How can I elegantly solve this or is my data modeling wrong? 回答1: This should work as expected. @email = Email.create(params[:email]

Rails order by associated data

佐手、 提交于 2019-12-11 17:34:39
问题 Is it possible to order the results of school.classrooms by the teacher's name? I want to do this directly in the association, and not a separate call. class School < ActiveRecord::Base has_many :classrooms end class Classroom < ActiveRecord::Base belongs_to :school belongs_to :teacher end class Teacher < ActiveRecord::Base has_one :classroom end 回答1: This should work if you are using rails 3.x school.classrooms.includes(:teacher).order("teachers.name") 来源: https://stackoverflow.com/questions

SQL Select Query with CodeIgniter's active records and 'where sha1' returns first row entry

笑着哭i 提交于 2019-12-11 17:33:42
问题 I duplicated this problem on a completely clean installation of CodeIgniter 2.0.2 The following code was added to the default 'welcome' controller, and the I created a 'test_model' to demonstrate the error. The following can be assumed/changes to config/autoload/database: - correct definition of base url - autoload database library - logging set to 4 (all messages) - database connection parameters This is what I added to the controllers/welcome.php: function test_me() { $this->load->model(

Rails 3 refuses to eager load

試著忘記壹切 提交于 2019-12-11 17:33:18
问题 I'm working in Rails 3.0.7. I have some many-to-many and some one-to-many associations that fail to eager load. My associations are: Person has_many :friends Person has_many :locations through=> :location_histories Location belongs_to :location_hour Location_hour has_many :locations In my controller I have the following: @people = Person.includes([[:locations=>:location_hour],:friends]).where("(location_histories.current = true) AND (people.name LIKE ? OR friends.first_name LIKE ? OR friends

codeigniter active records group_by returns random ordering of joined table record id

百般思念 提交于 2019-12-11 17:25:29
问题 I'm using Codeigniter, what I want to do is a query that returns joined table (item_gallery) gallery_id is lowest value. while main query ordered by desc of items post_date the below code group_by select random value of item_gallery gallery_id. but i want the lowest value of item_gallery gallery_id. public function shopItems($id) { $this->db->select("*"); $this->db->from('items'); $this->db->join('item_gallery', 'items.id = item_gallery.item_id', 'left'); $this->db->where('items.user_id', $id