ruby on rails - Why do I get a behavior difference when calling .last on has_many through associations? -


i'm pretty new rails dev. have quite few has_many through associations in project , they've worked expected when call .last until recent has_many through relationship created.

setting breakpoint , calling values in console expect following query:

(byebug) current_user.account_states.last

#<accountstate id: 1, state: 0, description: "account ok", created_at: "2016-01-25 19:19:36", updated_at: "2016-01-25 19:19:36"> 

but when make similar query on new has_many relationship not want. first of all, issues sql query, curious me. second, , more importantly, not give result i'm looking because sorts on dropsite id. expected results "my store" rather "janell's". see following:

(byebug) current_user.dropsites.last

  dropsite load (0.5ms)  select  "dropsites".*    "dropsites" inner join "user_dropsites"    on "dropsites"."id" = "user_dropsites"."dropsite_id"    "user_dropsites"."user_id" = ?     order "dropsites"."id" desc    limit 1  [["user_id", 6]] 

#<dropsite id: 3, name: "janell's", phone: "2065551212", hours: "7-7, m-f", address: "1234 main st.", access_instructions: "just walk in", created_at: "2016-01-25 21:09:10", updated_at: "2016-01-27 19:30:07", active: true, city: "boise", state: "id", zip: 83701> 

i able data wanted using:

dropsite.find(current_user.dropsites.joins(:user_dropsites).order('user_dropsites.created_at').last.id) 

but i'm wanting better understand why rails exhibits different behavior in these 2 cases can control code better.

i'm using rails 4.2.0 , ruby 2.2.2p95. i've tried searching hi , lo can't seem find information i'm lacking able figure 1 out on own. pointers/ideas appreciated!

here models:

class user < activerecord::base   has_many :user_account_states   has_many :account_states, through: :user_account_states   has_many :user_dropsites   has_many :dropsites, through: :user_dropsites end  class accountstate < activerecord::base   has_many :user_account_states   has_many :users, through: :user_account_states end  class useraccountstate < activerecord::base   belongs_to :account_state   belongs_to :user end  class dropsite < activerecord::base   has_many :user_dropsites   has_many :users, through: :user_dropsites end  class userdropsite < activerecord::base   belongs_to :dropsite   belongs_to :user   end 

here table data:

accountstate table data: "id", "state", "description", "created_at", "updated_at" "1", "0", "account ok", "2016-01-25 19:19:36.622125", "2016-01-25 19:19:36.622125" "2", "1", "account on hold", "2016-01-25 19:19:36.639550", "2016-01-25 19:19:36.639550"  useraccountstate table data: "account_state_id", "user_id", "created_at", "updated_at", "notes" "2", "6", "2016-01-27 18:08:55.593669", "2016-01-27 18:08:55.593669", "" "1", "6", "2016-01-27 18:13:44.831515", "2016-01-27 18:13:44.831515", ""  dropsite table data: "id", "name", "phone", "hours", "address", "access_instructions", "created_at", "updated_at", "active", "city", "state", "zip" "1", "my store", "213-412-5709", "24x7x365", "5719 54rd court ne", "", "2016-01-25 21:00:21.318115", "2016-01-27 19:29:29.300511", "t", "tallahassee", "fl", "32300" "3", "janell's", "2065551212", "7-7, m-f", "1234 main st.", "just walk in", "2016-01-25 21:09:10.337827", "2016-01-27 19:30:07.106183", "t", "boise", "id", "83701"  userdropsite table data: "user_id", "dropsite_id", "created_at", "updated_at" "6", "3", "2016-01-26 21:43:10.154020", "2016-01-26 21:43:10.154020" "6", "1", "2016-01-27 18:24:45.927775", "2016-01-27 18:24:45.927775" 


Comments

Popular posts from this blog

authentication - Mongodb revoke acccess to connect test database -

r - Update two sets of radiobuttons reactively - shiny -

ios - Realm over CoreData should I use NSFetchedResultController or a Dictionary? -