activerecord

Rails: Creating an “All activity feed” from multiple models

∥☆過路亽.° 提交于 2019-12-21 21:34:00
问题 I have a page that has several "Feeds". In my controller I have something like this: def index @plays = current_user.plays.includes(:game).order("created_at desc") @wants = current_user.wants.includes(:game).order("created_at desc") @ratings = current_user.ratings.includes(:game).order("created_at desc") end and I use code like this in the view: <% @plays.each do |play| %> You played <%= play.game.name %></p> <% end %> Now I want to make a forth feed on the page that is "all activity" which

rails paperclip no direct access through sql query from another model

风流意气都作罢 提交于 2019-12-21 20:44:00
问题 if tried to access paperclip images from another model to show in its view through a sql query, it wont show the images. i tried something like this from a category controller wch takes in params from a form, in the index page, thru select box. category controller def show @category = Category.find_by_sql ["select distinct l.* from listings l , categories c, categories_listings cl where c.id = cl.category_id and l.id = cl.listing_id and c.id in (?,?)" , params[:c][:id1] , params[:c][:id2]]

Extending the CI_DB_active_record class in codeigniter 2.0

拟墨画扇 提交于 2019-12-21 20:31:33
问题 I am writing my first program with Codeigniter, and have run into a problem. I will start with a focused description of the problem and can broaden it if I need to: I need to write a multi-dimensional array to the DB and want to use the insert_batch function from the CI_DB_active_record class to do so. The problem is that I need to write empty values as NULL for some fields while other fields need to be empty strings. The current function wraps all values with single quotes, and I cannot find

undefined method `[]' for nil:NilClass for Rails model on new

感情迁移 提交于 2019-12-21 20:22:23
问题 I have a model Student and a controller Students. In the controller I have a new method def new @student = Student.new end I then have a /views/students/new.html.haml that freaks out by using the @student. I get the error undefined method `[]' for nil:NilClass This is the haml for it %h1 Students#new %p Find me in app/views/students/new.html.haml = form_for(@student) do |f| = f.text_field :email = f.button "Submit" If I replace @student with Student.new I get the same results. However if I

How to model a mutual friendship in Rails

♀尐吖头ヾ 提交于 2019-12-21 20:01:32
问题 I know this question has been asked before on Stack Overflow, but the answers aren't doing it for me in ways I can explain. My general approach was inspired by this tutorial. What I'm trying to do, is create a really simple model for friending users that creates an equivalent friendship on both ends with a single record. At the db level, I just have a 'friendships' table that just has a user_id, a friend_id, and an is_pending boolean column. In user.rb I've defined the relationship as: has

ActiveRecord joins: integer columns converted to strings

牧云@^-^@ 提交于 2019-12-21 19:26:05
问题 I have a model named Article, which I am joining with TwitterShare as shown below: articles = Article.joins("LEFT OUTER JOIN twitter_shares ON articles.id = twitter_shares.article_id").where("articles.id = ? or articles.id = ?", 27165, 5632).select("articles.id, twitter_shares.user_id") When I get the articles back, and examine the Article model returned, it returns the user_id as a string, even though the column type in the twitter_shares table is an integer. Why is this? How can I make

ActiveRecord :includes - how to use map with loaded associations?

浪子不回头ぞ 提交于 2019-12-21 18:31:12
问题 I have a small rails app, and I'm trying to get some order statistics. So I have an Admin model, and an Order model, with one-to-many association. class Admin < ActiveRecord::Base attr_accessible :name has_many :orders class Order < ActiveRecord::Base attr_accessible :operation belongs_to :admin And I'm trying to get specifical orders using this query: admins = Admin.where(...).includes(:orders).where('orders.operation = ?', 'new gifts!') That works just as expected. But when I try to make

yii2 ActiveRecord findBySql - Response content must not be an array Error

爱⌒轻易说出口 提交于 2019-12-21 17:37:01
问题 New to Yii2 nuances. Just trying to get a return from a ActiveRecord query. I realize there is probably a much easier way to do this using Yii2 conventions public function actionGet_permissions() { $sql = 'select * from auth_item where owner_user_id IS NULL'; return Auth_Item::findBySql($sql)->all(); } Errors "Response content must not be an array." I think its pretty obvious the simple set of records I'm trying to return with this function. Any help is much appreciated, and let me know if

How to show sql result on server console

心已入冬 提交于 2019-12-21 17:18:21
问题 In Rails v2.3 , Ruby 1.8 , if I run a sql statement with following code in my model class: ActiveRecord::Base.connection.execute("select count(*) from cars;") How can I show the query result in server console? I tried : rslt = ActiveRecord::Base.connection.execute("select count(*) from cars;") p rslt but it only returns me "MySQL result object" on the server console, not the exact result. 回答1: There are couple ways to get mysql "answer" from your query. you can call each and it will iterate

Skip validation for related objects - rails activerecord

拟墨画扇 提交于 2019-12-21 17:11:59
问题 class Author has_many :books validates :email, :presence => true end class Book belongs_to :author validates :title, :presence => true end Skipping validations is easy: a = Author.new a.save(:validate => false) However, I need skip author validations when creating a book without skipping books validations, like this: b = Book.new b.title = "A Book" b.author = Author.last b.save 回答1: I quite did not understand your question. In your example you are not creating any new author object: > b =