Rails Upgraded syntax error, unexpected '\n', expecting => (SyntaxError) - ruby-on-rails-4

I have this query
has_many :unused_invitations, :class_name => 'Invitation', :foreign_key => 'inviter_id', :conditions => 'used = false'
I was using rails 3.2.17 and now I am upgrading to rails 4.0.4. I got this error
DEPRECATION WARNING: The following options in your User.has_many :unused_invitations declaration are deprecated: :conditions. Please use a scope block instead. For example, the following:
has_many :spam_comments, conditions: { spam: true }, class_name: 'Comment'
should be rewritten as the following:
has_many :spam_comments, -> { where spam: true }, class_name: 'Comment'
I solve it by modifying query
has_many :used_invitations, class_name: 'Invitation', foreign_key: 'inviter_id', -> { where used: false}
But Still I getting syntax error
syntax error, unexpected '\n', expecting => (SyntaxError)
What is wrong with query ? Will someone explain me about it. I have go this question but can't find the answer.

Solve this problem by updating the query
has_many :used_invitations, -> { where used: false}, class_name: 'Invitation', foreign_key: 'inviter_id'

Related

SyntaxError on has_many association with block when trying to order

in Ruby on Rails 4, let's say a parent has many children. Then I wanted to reference only the persisted records in an active record association, and followed the link's accepted answer. This works good:
class Parent < ActiveRecord::Base
has_many :children, dependent: :destroy do
def persisted
collect { |a| a if a.persisted? }
end
end
Now, I want to order the associated records:
has_many :children, dependent: :destroy, -> { order 'id asc' } do
but this raises an error:
SyntaxError in ParentsController#index
...trunk/app/models/parent.rb:3: syntax error, unexpected keyword_do_block, expecting => ...trunk/app/models/parent.rb:49: syntax error, unexpected keyword_end, expecting end-of-input
However, this does work:
has_many :children, -> { order 'id asc' } do
I can't even find documentation on how to use the do_block on an association. Any help appreciated.
There was 2 problems with my approach:
The order of the arguments. In rails 4, the scope goes before the options.
The options hash should be wrapped, because it isn't the last argument anymore.
So, this code does work:
has_many :children, -> { order 'id asc' }, { dependent: :destroy } do
def persisted
collect { |a| a if a.persisted? }
end
end
References:
https://www.ruby-forum.com/topic/5436931
http://apidock.com/rails/ActiveRecord/Associations/ClassMethods/has_many#461-User-a-block-to-extend-your-associations

Rails test ActiveRecord error

I initiated the test environment in my rails app, and when I test the user model with the default code, it throws the following error:
Test code:
test "the truth" do
assert true
end
1) Error:
UserTest#test_the_truth:
ActiveRecord::RecordNotUnique: Mysql2::Error: Duplicate entry '' for key 'index_users_on_email': INSERT INTO `users` (`created_at`, `updated_at`, `id`) VALUES ('2014-02-01 17:45:51', '2014-02-01 17:45:51', 298486374)
and inside my user model, I have the following associations
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
validates :user_name , :email, :first_name ,:last_name , :presence => true
has_many :invitations
has_many :incoming_friends, -> { where(:status => '1') }, :class_name => "User", :foreign_key => "friend_id", :through => :invitations
has_many :outgoing_friends, -> { where(:status => '1') }, :class_name => "User", :foreign_key => "user_id", :through => :invitations
Firstly, check your user model fixture in test/fixtures/users.yml. If you have empty declarations of one and two:
one: {}
# column: value
#
two: {}
# column: value
it can cause a problems, because there is a lack of attributes. Remove this part or comment it:
#one: {}
# column: value
#
#two: {}
# column: value
And try run it again.

Rails 4 routes with single table inheritance and self references

I've been jumping between design patterns, firstly trying polymorphic, now landing on STI. The main goal is to implement a Server > Host > Guest model where a Server has Hosts, Hosts have Guests and each able to have Posts. Although not the main purpose of the question any ideas in the design matter would be helpful as this is my first rails or ruby project.
What I have now is:
class Device
has_may :children, :class_name => "Device", :foreign_key => "parent_id"
belongs_to :parent, :class_name => "Device"
has_many :posts
end
class Server,Host,Guest < Device
end
STI is used because Server,Host,Guest basically have the same attributes.
I'm having trouble setting up the routes and controllers so I could view a Server's children which would be of type Host or to create a new Server's Host.
First, a good thing would be to add the following things, making everything easier to use for you :
class Device < ActiveRecord::Base
has_many :children, :class_name => "Device", :foreign_key => "parent_id"
has_many :servers, -> { where type: "Server" }, :class_name => "Device", :foreign_key => "parent_id"
has_many :hosts, -> { where type: "Host" }, :class_name => "Device", :foreign_key => "parent_id"
has_many :guests, -> { where type: "Guest" }, :class_name => "Device", :foreign_key => "parent_id"
belongs_to :parent, :class_name => "Device"
has_many :posts
end
With that, you will be able to do server.hosts, etc, which is quite convenient.
Then, you should move each subclass (Server, Host, Guest) to its own file due to Rails loading system. You can try to access the model Server in the console, you will get an undefined error. To fix it, you need to load the model Device, or simply move each subclass in a different file.
Finally, for the routing/controller part, I will advise you to read this post I wrote about common controller for STI resources : http://samurails.com/tutorial/single-table-inheritance-with-rails-4-part-2/.
Note that this is the second part, for more details check out the other articles.

rails 4 how to use foreign key and primary key

How to set the following association:
class Midatum < ActiveRecord::Base
# ..., diagn1, diagn2, diagn3
# sample data:
# ..., "0123", nil ,"0124"
# ..., "0123", nil ,"0124"
# ..., "0123", "1123", nil
belongs_to :icd9, :foreing_key => :diagn1
belongs_to :icd9, :foreing_key => :diagn2
belongs_to :icd9, :foreing_key => :diagn3
end
class icd9 < ActiveRecord::Base
# icd9, description
# sample data:(unique)
#"0123", "some text"
#"0124", "some other text"
#"1123", "description text"
#"1133", "description text"
has_many :midata, :foreing_key => :icd9, :primary_key => :icd9
end
This does not work. It may be obvious for someone but not for me. The database
is a legacy DB and readonly. I need to establish this assoc to able to work with the data.
This answer comes from a Rails expert and it does solve my problem. I am posting it in case someone else have the same problem.
belongs_to :icd9_a, :foreign_key => :diagn1, :class_name => "Icd9"
belongs_to :icd9_b, :foreign_key => :diagn2, :class_name => "Icd9"
belongs_to :icd9_c, :foreign_key => :diagn3, :class_name => "Icd9"
But that means you'll need to query the association using all three methods:
m = Midatum.first
m.icd9_a
m.icd9_b
m.icd9_c
In the same way, over in the Icd9 class you'll need three separate associations with unique names:
class Icd9 < ActiveRecord::Base
self.primary_key = :icd9
has_many :midata_a, :foreign_key => :diagn1, :class_name => "Midatum"
has_many :midata_b, :foreign_key => :diagn2, :class_name => "Midatum"
has_many :midata_c, :foreign_key => :diagn3, :class_name => "Midatum"
end
Note also that since the icd9 table doesn't have an 'id' column but uses the 'icd9' column as the primary key, you'll need to set it as I've done:
self.primary_key = :icd9

Deprecated warning with has_many_through statement in Rails 4

I have the following relationship
class User
has_many :relationships
has_many :friends, :through => :relationships, -> select: 'friends.*, relationships.weight', order: 'weight DESC'
When I upgrade to Rails 4, I got the following warning:
DEPRECATION WARNING: The following options in your Service.has_many :friends declaration are deprecated: :order,:select.
How should I fix this? In general, is there a working in progress reference for Rails 4?
In Rails 4, any of the options that you would see in a normal User.where(...) style query would now go in a lambda. This includes :order and :select:
has_many :friends, -> { select("friends.*, relationships.weight").order("weight desc") }, :through => :relationships
Note that the Proc does need to be the second parameter to has_many, so the :through => part needs to stay at the end.