activerecord

How to update 'status' value in table on each button click

泄露秘密 提交于 2019-12-13 16:14:10
问题 I want to update the status value in the properties table as (1 or 2 or 3 or 4) when I click on each button. These are my buttons in my view file: <td><%= link_to("Waiting for Response", rms_property_approve_property_path(property, {:status => 'Waiting for Response'}), method: :patch, class: "btn btn-success", "data-no-turbolink" => true) %><td> <td><%= link_to("No Response", rms_property_approve_property_path(property, {:status => 'No Response'}), method: :patch, class: "btn btn-danger",

Rails Query to return users belongs to any cities & not belong to any cities

若如初见. 提交于 2019-12-13 15:55:57
问题 I have Many to Many Associations between two tables: For Ex users & cities users id name 1 Bob 2 Jon 3 Tom 4 Gary 5 Hary cities id name 1 London 2 New-york 3 Delhi users_cities id user_id city_id 1 1 2 2 2 1 3 3 1 4 3 2 5 4 3 I want two sql queries Query which accepts array of city_id and return all the users belongs to that cities. For Ex when city_id : [1, 2] then result should be O/P should be id name 1 Bob 2 Jon 3 Tom Query which accepts array of city_id and return all the users who do

In Rails ActiveRecord, joins does not work with has_and_belongs_to_many in namespaced models

强颜欢笑 提交于 2019-12-13 15:05:04
问题 I have two models in a namespace, a service and an instructor, both which have a many to many relationship with each other, defined through a has_and_belongs_to_many: class Scheduling::Service < ActiveRecord::Base has_and_belongs_to_many :instructors end class Scheduling::Instructor < ActiveRecord::Base attr_accessible :first_name, :last_name has_many :instructor_availabilities scope :of_service, lambda { |service_id| joins(:services).where(:services => {:id => service_id})} has_and_belongs

Render non-activerecord objects in JSON/XML [RoR]

扶醉桌前 提交于 2019-12-13 14:40:15
问题 I am testing a small whois API using the ruby gem whois, and due to the format of whois responses being quite funny sometimes i was asked not to use ActiveRecord for saving the responses. Simply put, here's how it works : User inputs a domain name in a form from a view (action 'lookup' = creating a request) Controller catches the parameter then sends to model (non activerecord) by instanciating the request object [containing the whois response] Model uses the whois gem and sends back to

Can't mass-assign protected attributes: user

。_饼干妹妹 提交于 2019-12-13 14:09:24
问题 I'm working on a simple app that requires me to submit a form. I created two models. user.rb class User < ActiveRecord::Base attr_accessible :email has_many :item end item.rb class Item < ActiveRecord::Base attr_accessible :user_id belongs_to :user end Instead of creating a user using the user form view , I'm trying to create the user using the item form view . items/_form.html.haml = nested_form_for @item do |form| = form.fields_for :user do |builder| = builder.text_field :email = form

Simple persistence framework compatible with macruby?

蓝咒 提交于 2019-12-13 13:23:14
问题 I'm running into problems using macruby with ActiveRecord (w/ sqlite3) or Sequel. Any other suggestions? I need a simple lightweight persistence mechanism to embed within my application that can handle less than 5 tables and at most low tens of thousands of rows. 回答1: The following combination works: MacRuby 0.8 sqlite3-ruby gem 1.3.2 sequel gem 3.18.0 OSX 10.6.5 The trick was uninstalling the 'sqlite3' gem and installing 'sqlite3-ruby'. 回答2: This also works MacRuby 0.12 sqlite3-ruby gem 1.3

how to access column value after serialization activerecord

痞子三分冷 提交于 2019-12-13 12:54:59
问题 I have a simple model like this: class User < ActiveRecord::Base serialize :preferences end I want to access the raw value from mysql, not value before serialize. Is it possible? I know I can use ActiveRecord::Base.connection.execute("select * from users") But I want to access from the User model. 回答1: Updated Ok, this is what you are looking for: User.find(params[:id]).attributes_before_type_cast["preferences"][:value] This will return the string in its serialized form. That is the closest

Different date saved to database - wrong time zone

ⅰ亾dé卋堺 提交于 2019-12-13 12:42:49
问题 When I fetch a date from FullCalendar with Javascript I have the following values: Fri Sep 13 2013 08:30:33 GMT-0400 (EDT) After I save that date to database I see that in database there are different values: 2013-09-13 12:00:00.000000 So, date is automatically converted to UTC and saved like that into database. How to save the correct date into database? I don't want to hardcode it :) Thank you. 回答1: For that you need to define your time zone in your application.rb config.time_zone = 'YOUR

Validate on inclusion within array of options OR be nil

大城市里の小女人 提交于 2019-12-13 11:57:04
问题 I have a model where I'd like to restrict input for a field to either be nil or fall within a specified array of values. I can get the inclusion part working, but the allow_nil: true bit doesn't seem to work for me: class Mock::Patient < ActiveRecord::Base LANGUAGE_OPTIONS = %w[English Spanish French German Chinese Hindi Punjabi] validates :preferred_language, inclusion: { in: LANGUAGE_OPTIONS } end I've tried modifying that last line to things like: validates :preferred_language, inclusion:

How to test which validation failed in ActiveRecord?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-13 11:53:46
问题 I have a model like this: class User < ActiveRecord::Base validates_length_of :name, :in => (2..5) end I want to test this validation: it "should not allow too short name" do u = User.new(:name => "a") u.valid? u.should have(1).error_on(:name) end But then it does not test which kind of error was set on name . I want to know, if it was too_short , too_long , or maybe some other validation failed. I can lookup the message text in errors array, like this: u.errors[:name].should include(I18n.t(