scoped collection doesn't return correct origin and origin_attribute
Reported by Tomoaki Hayasaka | October 25th, 2010 @ 12:16 PM | in Hobo 1.3 (Rails 3)
See:
irb(main):001:0> User.first.tweets.recent.origin # should return first user
=> nil
irb(main):013:0> User.first.tweets.recent.origin_attribute # should == :tweets
=> nil
Because of this, <aside>
in
<show-page>
causes
ActionView::Template::Error (The context is not an
ActiveRecord::Base instance or class. (this = nil))
.
Test:
class User < ActiveRecord::Base
hobo_user_model # Don't put anything above this
fields do
name :string, :required, :unique
email_address :email_address, :login => true
administrator :boolean, :default => false
timestamps
end
has_many :tweets
def create_permitted?; true; end
def update_permitted?; true; end
def destroy_permitted?; true; end
def view_permitted?(field); true; end
end
class Tweet < ActiveRecord::Base
hobo_model # Don't put anything above this
fields do
tweet :string
timestamps
end
belongs_to :user
def create_permitted?; true; end
def update_permitted?; true; end
def destroy_permitted?; true; end
def view_permitted?(field); true; end
end
class TweetsController < ApplicationController
hobo_model_controller
auto_actions :all
auto_actions_for :user, [:index, :new, :create]
end
class UserHints < Hobo::ViewHints
children :tweets, :tweets
end
and visit /users
.
Or for scopes.rdoctest:
>> Bryan.friends.origin.name
=> "Bryan"
>> Bryan.friends.order(:id).origin.name
=> "Bryan"
>> Bryan.friends.order_by(:id).origin.name
=> "Bryan"
>> Bryan.friends.origin_attribute
=> :friends
>> Bryan.friends.order(:id).origin_attribute
=> :friends
>> Bryan.friends.order_by(:id).origin_attribute
=> :friends
Comments and changes to this ticket
-
Domizio Demichelis October 25th, 2010 @ 03:16 PM
- State changed from new to open
- Milestone set to Hobo 1.3 (Rails 3)
- Assigned user set to Domizio Demichelis
- Milestone order changed from 197912 to 0
-
Domizio Demichelis October 26th, 2010 @ 01:02 AM
- State changed from open to resolved
(from [5c190e885651b60dc1964c848d36b562016863e3]) added origin and origin_attribute to ActiveRecord::Relation [#835 state:resolved] http://github.com/tablatom/hobo/commit/5c190e885651b60dc1964c848d36...
-
Domizio Demichelis October 26th, 2010 @ 03:04 PM
Yes, usually a method_missing should accept a block, but the original
method missing that it patches, does not, so I just copied it,
preserving the original errors too :-) -
Domizio Demichelis October 27th, 2010 @ 10:18 PM
(from [dfba4152529958fe31793e90fdf87d3cd940fcdf]) added clone to the scoped relation (just in case of caching somewhere sometime) [#835] http://github.com/tablatom/hobo/commit/dfba4152529958fe31793e90fdf8...
-
Domizio Demichelis October 31st, 2010 @ 01:42 PM
The method_missing in ActiveRecord::Associations::AssociationCollection ignores the block, so I really don't see any point to accept and pass along a block that will be totally ignored both by our method, and the original method. I am probably missing something, because your statements confuse me. Please explain me the following:
"That is a feature of active record" If it is a "feature", so why the original method_missing ignores the block?
"changing its behavior makes users get confused" IMHO we are NOT changing its behaviour BECAUSE we ignore the block like the original does
"Even the examples in the active record NamedScope documents will not be run as is." That really confuses me, since I cannot understand HOW our overriding can change the behaviour of the original method, unless our method has some bug on its own (but not for the block though)
-
Domizio Demichelis October 31st, 2010 @ 03:01 PM
Thank you, I have just realized that my statement "the original method ignores the block" was simply wrong. Indeed it uses block_given?, so it does not ignores the block.
Please Sign in or create a free account to add a new ticket.
With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป
People watching this ticket
Tags
Referenced by
- 835 scoped collection doesn't return correct origin and origin_attribute (from [5c190e885651b60dc1964c848d36b562016863e3]) added o...
- 835 scoped collection doesn't return correct origin and origin_attribute (from [dfba4152529958fe31793e90fdf87d3cd940fcdf]) added c...
- 835 scoped collection doesn't return correct origin and origin_attribute (from [c2fdb5418a0828b1852b62f3c67196f7add53079]) fixed t...