Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move_lower and move_higher not working returning nil #57

Closed
shaileshkalamkar opened this issue Oct 25, 2012 · 7 comments
Closed

move_lower and move_higher not working returning nil #57

shaileshkalamkar opened this issue Oct 25, 2012 · 7 comments

Comments

@shaileshkalamkar
Copy link

Hi,

I am using Rails 3.2.6 application with acts_as_list(0.1.6) gem included in my Gemfile.

I have the code as follows

class Product < ActiveRecord::Base
  attr_accessible :name, :price
  has_many :product_stores
  has_many :stores, :through => :product_stores, :order => 'product_stores.position'
end

class ProductStore < ActiveRecord::Base
  attr_accessible :position, :product_id, :store_id
  default_scope :order => 'position'

  belongs_to :store
  belongs_to :product

  acts_as_list :scope => :product
end

class Store < ActiveRecord::Base
  attr_accessible :description, :name
  has_many :product_stores
  has_many :products, :through => :product_stores, :order => 'product_stores.position'
end

I have position(integer) column in the product_stores table . But the move_lower and move_higher functions does not work returning nil and the product is not moved to any position without updating the position. e.g

ruby-1.9.2-p290 :008 > store.product_stores.where(:product_id => 2).first.move_lower
  ProductStore Load (0.2ms)  SELECT `product_stores`.* FROM `product_stores` WHERE `product_stores`.`store_id` = 1 AND `product_stores`.`product_id` = 2 LIMIT 1
  ProductStore Load (0.3ms)  SELECT `product_stores`.* FROM `product_stores` WHERE (`product_stores`.`product_id` = 2 AND position = 2) ORDER BY position LIMIT 1
 => nil 
ruby-1.9.2-p290 :009 > 

Hw to resolve it ?

@matthewfarrell
Copy link

I too had this problem after setting up acts_as_list when there was existing data in the table. It appears that move_lower/move_higher will not move an item if the position column value is null, or if only one item has a position. This makes sense, but is not immediately obvious. To resolve this, I set a default position for all items using a migration:

execute "UPDATE sortable_table SET position = id"

Source: http://nicholshayes.co.uk/blog/?p=344

@swanandp
Copy link
Contributor

@shaileshparamisoft I think upgrading to latest should solve your problem.

@swanandp
Copy link
Contributor

But if not, let me know.

@shaileshkalamkar
Copy link
Author

@swanandp Yes, the issue has been resolved.
Thanks for you reply.

@daz
Copy link

daz commented Mar 13, 2014

I'm still getting nil when setting it up, because all my position columns are nil.

@mkolodziej
Copy link

I had a Category hierarchy in my app and had the same problem with null position values. I solved it by adding this method to the migration that created the position column:

  update_position_recursively(Category.roots)

  def update_position_recursively(categories)
    categories.each_with_index do |cat, idx|
      cat.update_attribute(:position, idx)
      update_position_recursively(cat.subcategories) unless cat.subcategories.empty?
    end
  end

This is assuming Category.roots would return all roots and that I have has_many :subcategories ... in my Category class

@redbar0n
Copy link

I fixed this problem by making a rake task (as DB updates generally shouldn't be done in migrations). See: https://stackoverflow.com/a/15531843/380607

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants