activerecord

Yii Framework : Help to translate relational Sql query

自作多情 提交于 2019-12-11 15:58:46
问题 I encountered a problem to write this query in a Yii model oriented style. I have a 3 main tables : questions, categories, countries and relational tables : questions_has_categories, questions_has_countries. Now i try to search questions that belongs to country, category. With normal SQL statement i write : SELECT q.id, q.question, c.name, co.name FROM questions AS q, categories AS c, countries AS co, questions_has_countries AS ta, questions_has_categories AS qhc WHERE q.id = qhc.questions_id

rails 3 merge multiple queries from different tables

时间秒杀一切 提交于 2019-12-11 15:48:20
问题 i have the following queries in my controller. Is there any way that i can merge them into one? @record_videos = RecordVideo.where("recommand = ?",true).limit(8) @musics=Music.limit(13) @new_topics=Topic.limit(5).order("created_at desc") 回答1: No, you cannot combine multiple queries that return different classes of object. Well, you could , theoretically, if you wrote your own query SQL and used a few UNION s, but Rails would likely have no idea what to do with the resulting data. I can't,

has_many with foreign key stored in own table

ぐ巨炮叔叔 提交于 2019-12-11 15:46:40
问题 I have the model Parent that has_one Child . However, for some logical reason, I need to store foreign key in Parent instead of Child . Is it possible to define has_one relationship with foreign key in the Parent table? I don't want to define Parent belongs_to Child , because I want to create these objects from Parent by defining accepts_nested_attributes_for . 回答1: That is the inverse of has_one - that is belongs_to . See http://guides.rubyonrails.org/association_basics.html#choosing-between

in UsersController#create, User.new(params[:user]) return an empty User (params looks good)

删除回忆录丶 提交于 2019-12-11 15:25:42
问题 I'm kind of new to Rails 3.1. and I'm facing an issue only in my production env with my Signup form (actually, it's more about the controller). Here is the code in User class UsersController < ApplicationController [...] def create @user = User.new(params[:user]) logger.info "value of login in param : #{params[:user][:login]}" #-> log the actual login logger.info "value of login : #{@user.login}" #-> log empty @user.admin = false if @user.save flash[:notice] = t('flash.notice.user.create

Update join table HABTM

你。 提交于 2019-12-11 15:02:00
问题 I have a relationship between two models: House and Person class House has_and_belongs_to_many :persons end I have a join table like this: house_id | person_id | used 1 1 false I need to update used to "true" using this code: h = house.persons.find(params[:person_id]) h.update_attribute(:used, true) # ERROR used column doesn't exists in persons table How can I update the column used in join table ? Thanks. 回答1: I would recommend you use the has_many and belongs_to relationships between your

Yii ActiveRecord field only returns default value for recently added column

只愿长相守 提交于 2019-12-11 14:34:30
问题 Yii 1.1.12: When I select an instance of the User model, I get the default value for certain properties/columns. This happens for columns that I have recently added (manually) to the database: $user->locale // returns null (= default value) If I set a new, different default value in the database (via phpMyAdmin), I will receive that new default value. Other columns of that table do just fine: $user->email // returns the correct value I will receive the correct value if I run an SQL query:

rails: query and filter n:m related objects using active record

独自空忆成欢 提交于 2019-12-11 14:03:34
问题 I'm trying to use active-record query possible connections between airports. I described the models I created already in another question here: n:m self-join with ruby on rails active record Basically, what I can do now is that: ny = Airport.create({"city" => "New York"}) la = Airport.create({"city" => "Los Angeles"}) ny.destinations << la la.destinations << ny I ran into an issue querying the data I'm looking for, which is quite simple in SQL but I had no luck with active record yet. ny =

Rails: How to handle existing invalid dates in database?

我只是一个虾纸丫 提交于 2019-12-11 13:59:27
问题 First, this is directly related to my other question: How to gracefully handle "Mysql2::Error: Invalid date" in ActiveRecord? But I still do not want to jump through all the loops of writing migrations which fix dates. That won't be the last table with invalid dates and I need some more generic approach. So here we go: I'm using a legacy MySQL database which contains invalid dates, sometimes like 2010-01-00 or 0000-04-25... Rails does not load such records (older versions of Rails did). I do

Rails ActiveRecord intersect query with has_many association

半世苍凉 提交于 2019-12-11 13:58:55
问题 I have the following models: class Piece < ActiveRecord::Base has_many :instrument_pieces has_many :instruments, through: :instrument_pieces end class Instrument < ActiveRecord::Base has_many :pieces, through: :instrument_pieces has_many :instrument_pieces end class InstrumentPiece < ActiveRecord::Base belongs_to :instrument belongs_to :piece end And I have the following query: Piece .joins(:instrument_pieces) .where(instrument_pieces: { instrument_id: search_params[:instruments] } ) .find

query: has_many where all the child fields are nil

安稳与你 提交于 2019-12-11 13:52:55
问题 I'm using Rails 3.2 with ActiveRecord and MySQL and I have models with one to many association: class Author has_many :books end class Book belongs_to :author attr_accessible :review end I want to find authors that have all the books without review. I tried: Author.includes(:books).where('book.review IS NIL') but is obviously didn't work, because it finds authors that have at least one book without review. What query should I use? 回答1: SQL is quite simple: SELECT authors.name, count(books