I have been using a gem called ajaxful-rating for a while. Now, when I upgrade to Rails 3.1, I get the following error:
/Users/nn/.rvm/gems/ruby-1.9.2-p290/gems/ajaxful_rating-2.2.9.1/lib/axr/model.rb:23:
syntax error, unexpected ',', expecting tASSOC (SyntaxError)
/Users/nn/.rvm/gems/ruby-1.9.2-p290/gems/ajaxful_rating-2.2.9.1/lib/axr/model.rb:27: syntax error, unexpected keyword_do_block, expecting keyword_end
options[:dimensions].each do |dimension|
Original code is in the link above, but the relevant part is as follow:
def ajaxful_rateable(options = {})
has_many :rates_without_dimension, :as => :rateable, options.merge(:class_name => 'Rate'),
:dependent => :destroy, :conditions => {:dimension => nil}
has_many :raters_without_dimension, :through => :rates_without_dimension, :source => :rater
options[:dimensions].each do |dimension|
has_many "#{dimension}_rates", :dependent => :destroy,
:conditions => {:dimension => dimension.to_s}, :class_name => 'Rate', :as => :rateable
has_many "#{dimension}_raters", :through => "#{dimension}_rates", :source => :rater
end if options[:dimensions].is_a?(Array)
What is causing this issue and how can I fix it?
Thank you.
You have to change hash syntax like below:
:conditions => {:dimension => nil} TO :conditions => [:dimension => nil]
Can you try with above.
Means you have to change HASH syntax.
Related
Validates_format_of :email example available on api.rubyonrails.org is throwing errors.
class Person < ActiveRecord::Base
validates_format_of :email, :with => %r\A([^#\s]+)#((?:[-a-z0-9]+.)+[a-z]{2,})\Z/, :on => :create
end
validates :email, :format => { :with => %r\A([^#\s]+)#((?:[-a-z0-9]+.)+[a-z]{2,})\Z/, :on => :create }
Currently i am using rails 3.2.8 versions.
On loading getting the error as
"syntax error, unexpected ']', expecting keyword_end"
validates_format_of :email, :with =>
/\A([^#\s]+)#((?:[-a-z0-9]+.)+[a-z]{2,})\Z/i, :on => :create
Can't tell what's wrong here. I'm trying to filter on the state property of my address association.
class Organization < ActiveRecord::Base
has_one :address, :as => :addressable, :dependent => :destroy
define_index do
indexes :name, :sortable => true
has mec_revenue
has 'CRC32(status)', :as => :status, :type => :integer
has 'CRC32(address.state)', :as => :state, :type => :integer
set_property :delta => true
end
end
But when I run rake ts:index, I get the error below.
indexing index 'organization_delta'...
ERROR: index 'organization_delta': sql_range_query: ERROR: missing FROM-clause entry for table "address"
LINE 1: ...S "mec_revenue", CRC32(status) AS "status", CRC32(address.st...
Any ideas?
I'm not sure how to fix the FROM-clause problem, but if I use the table name instead of the property name, I can fix this with a group_by clause.
has 'CRC32(addresses.state)', :as => :state, :type => :integer
group_by "addresses.state"
I have a polymorphic asset model but would like two different versions of it for an asset class. Something like:
class Location < ActiveRecord::Base
has_many :assets, :as => :assetable, :order => 'position'
has_many :assets :known_as => :cover_images, :as => :assetable, :order => 'position'
I saw this question: Rails Polymorphic Association with multiple associations on the same model but when I implemented in Rails 3.1 with my models, it looked like:
has_many :assets, :as => :assetable, :order => 'position'
has_one :header_photo, :class_name => 'Asset', :as => 'assetable', :conditions => {:assetable_type => 'header_asset'}, :dependent => :destroy
Looking in console, I see:
SELECT `assets`.* FROM `assets` WHERE `assets`.`assetable_id` = 37 AND `assets`.`assetable_type` = 'Location' AND `assets`.`assetable_type` = 'header_asset' LIMIT 1
which is not what I want.
How would I do this? Is there syntax for handling this?
thx
Using Rails 3.1 RC4.
My User model has the following:
has_many :emails, :dependent => :destroy
accepts_nested_attributes_for :emails
My Email model has the following:
belongs_to :user
attr_accessible :email, :email_confirmation, :as => :admin
In Rails console:
User.first.update_attributes!({:artist_name => 'foo', :emails_attributes => {0 => {:email => 'foo#blah.com', :email_confirmation => 'foo#foo.com'}}}, :as => :admin)
I get:
WARNING: Can't mass-assign protected attributes: email, email_confirmation
In my Email model, if I remove :as => :admin. Everything works...
Should I be assigning some kind of scope to accepts_nested_attributes_for? Anyone know how this can be fixed?
Issue and solution has been highlighted here.
In summary, an options hash must be passed.
I am trying to create scopes to find all Galleries by a specific category type, like "Style". Eventually, they will be chained to filter by multiple category types, but I can't get the first to work.
Here are the models:
Gallery:
has_many :gallery_categories, :class_name => "GalleryCategories", :dependent => :destroy
has_many :categories, :through => gallery_categories
has_many :colors, :through => gallery_categories, :source => :category, :conditions => {:type => "Color"}
has_many :styles, :through => gallery_categories, :source => :category, :conditions => {:type => "Style"}
...and many more types of categories...
Category:
:has_many :gallery_categories
:has_many :galleries, :through => :gallery_categories
GalleryCategories:
:belongs_to :gallery
:belongs_to :category
I am trying to do something like this in Gallery:
:scope :by_style, lambda {|style| joins(:styles).where(:category => {:name => style})}
Then, for example, I run
Gallery.by_style("Contemporary")
And I am returned 181 Galleries when there are only 40 Galleries, and in this example there should only be one returned with the style "Contemporary".
Here is the resulting SQL:
SELECT `galleries`.* FROM `galleries` INNER JOIN `gallery_categories` ON `galleries`.`id` = `gallery_categories`.`gallery_id` INNER JOIN `categories` ON `categories`.`type` = 'Style' WHERE `categories`.`name` = 'Contemporary'
Any ideas? Thanks in advance.