activerecord

Ruby on rails, Change column value on update of other column value

余生长醉 提交于 2019-12-12 11:15:56
问题 I have two columns related to each other in a Rails model: Article.body Article.body_updated_on I want to change the Article.body_updated_on to Time.now , every time Article.body is updated. If any other fields updated nothing needs to be happen. 回答1: Just add before save callback to your Article model class Article < ActiveRecord:Base before_save :update_body_modified private # <--- at bottom of model def update_body_modified self.body_updated_on = Time.now if body_changed? end end 回答2: You

Rails: Virtual attributes and form values

纵饮孤独 提交于 2019-12-12 11:13:42
问题 I have a Model Book with a virtual attribute for create a Editor from the Book form. The code looks like: class Book < ActiveRecord::Base has_many :book_under_tags has_many :tags, :through => :book_under_tags has_one :editorial has_many :written_by has_many :authors, :through => :written_by def editorial_string self.editorial.name unless editorial.nil? "" end def editorial_string=(input) self.editorial = Editorial.find_or_create_by_name(input) end end And the new form: <% form_for(@book,

Yii2 : Active Record Column Aliases

纵然是瞬间 提交于 2019-12-12 11:03:04
问题 I'm using Yii2 framework with advanced template. I get problem with column alias in my controller file, here's my code: $models = new ActiveDataProvider([ 'query' => User::find()->select(['member'=>'fullname']) ]); The above query equivalent with: SELECT fullname AS member FROM User; I send the data to the view using this code: return $this->render('view', [ 'model' => $models, ]); I want to call the data in my view using GridView widget, here's my code: echo GridView::widget([ 'dataProvider'

RecordNotFound raised when using find_by_id to get non-existent record in RSpec

我的未来我决定 提交于 2019-12-12 10:48:16
问题 I've written this spec in products_controller_spec.rb, that is intended to test a redirect when destroy is called on a non-existent record: it "deleting a non-existent product should redirect to the user's profile page with a flash error" do delete :destroy, {:id => 9999} response.should redirect_to "/profile" flash[:error].should == I18n.t(:slideshow_was_not_deleted) end Here's the controller action in products_controller.rb: def destroy product = Product.find_by_id(params[:id]) redirect_to

Homebrew / TinyTDS / FreeTDS bundle error

我怕爱的太早我们不能终老 提交于 2019-12-12 10:46:15
问题 I used homebrew (linux port) to install freeTDS and am trying to deploy /bundle install my app with gem tinytds. I get this error output when doing bundle install Installing tiny_tds (0.5.1) with native extensions Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension. /home/deployer/.rbenv/versions/1.9.2-p290/bin/ruby extconf.rb /usr/bin/ld: /opt/homebrew/lib/libsybdb.a(dblib.o): relocation R_X86_64_32 against `.rodata' can not be used when making a shared object;

Rails: Can you pass arguments to the after_create method?

狂风中的少年 提交于 2019-12-12 10:44:52
问题 In my application, only users with the administrator role may create new users. In the new user form, I have a select box for each of the available roles that may be assigned to new users. I am hoping to use the after_create callback method to assign the role to the user. How can I access the selected value of the select box in the after_create method? def create @user = User.new(params[:user]) respond_to do |format| if @user.save flash[:notice] = 'User creation successful.' format.html {

remove field name from object validation message

寵の児 提交于 2019-12-12 10:39:08
问题 I've got a simple active record validation on an object using this within a form: form.error_messages({:message => '', :header_message => ''}) This in turn outputs something like "FieldName My Custom message" What i need to do is remove the field name from the error message but leave my custom message. Can anyone point me in the right direction for this. 回答1: One way to have complete control over the messages is to use a custom validate block in the model. e.g. to check that a field is not

How to issue a 'find' or 'where' that raises a RecordNotFound

爱⌒轻易说出口 提交于 2019-12-12 10:37:14
问题 When I call a find with an id, it becomes a targeted find, and will throw an error RecordNotFound. Foo::Bar.find(123) # RecordNotFound if no Bar with id 123 exists. But when I call that with conditions, I get nil if not found: Foo::Bar.find(:first, :conditions => [ "lower(name) = ?", name.downcase ]) I want such a conditional search to raise an error too. I know I can do: Foo::Bar.find_by_name!("CocktailBar") #=> raises Recordnotfount if not not found. But that has only really simple

Rails - how to search based on a boolean field? (MySQL error)

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-12 10:32:33
问题 I'm running the following query @projects = @company.projects.where("active = ?", true).order("created_at ASC") and I'm getting the error: `ActiveRecord::StatementInvalid: Mysql::ParseError: You have an error in your SQL...` the error points to the = '1' . I've tried many variations on my query but I cannot figure out the problem. How can I solve this? 回答1: You don't need to use parameterized queries with literals, just do this: @projects = @company.projects.where("active = 1").order("created

ActiveRecord search returns 'Syntax error or access violation' error

懵懂的女人 提交于 2019-12-12 09:59:58
问题 In my Yii application, I have a model that represents siteconfig table and have four columns: integer config_id , string key , string value , string update_time . I created a model using Gii (to ensure that I will not make any mistakes). I don't publish entire code here, cause this is 100% unmodified by me, standard model code generated by Gii. Since my problem is related to search, I only publish important part of generated code (the search() method): public function search() { // Warning: