4

Hi I have a model called "Listing". Here is the constructor for the model:

def initialize(business)
    puts 'inside Listing.initialize'

    @name = business.name
    @telephone = business.telephone


    puts 'Created a new Listing'
end

I have a controller called "listings_controller" I have is another model called "Business". Inside the "listing_controller" I have a method in which I would like to instantiate a "Listing" with attributes of a "Business".

Here is the code that does that in a "listings_controller"

def create_listings

    self.get_all  
    @businesses.each do |business|
     Listing.create(business)

    end

end


def show

   self.create_listings
   @listings = Listing.all

   respond_to do |format|
   format.html #show.html.erb
   end

end

This initialization method is not working.Im getting this exception:

wrong number of arguments (2 for 1)

Rails.root: /Users/AM/Documents/RailsWS/cmdLineWS/Businesses

Application Trace | Framework Trace | Full Trace app/models/listing.rb:53:in initialize' app/controllers/listings_controller.rb:18:inblock in create_listings' app/controllers/listings_controller.rb:17:in each' app/controllers/listings_controller.rb:17:increate_listings' app/controllers/listings_controller.rb:26:in `show'

How can I fix this? Thanks

6
  • Why do you copy the values instead of associating the Business to the Listing? Commented Mar 12, 2012 at 1:53
  • business and listing are not one to one in terms of attributes. Listing has more attributes than a business Commented Mar 12, 2012 at 1:55
  • this is really a question of application design, but the object oriented way would be to have a Business instance as an attribute of the Listing object. Listing could have additional attributes but the original Business attributes would only reside in one place (DRY). Commented Mar 12, 2012 at 2:00
  • I have 2 databases inside my rails app. One is a legacy the other is the app's DB. The model Business is pulling data from the legacy DB, Im trying to transfer all the data from the legacy, clean it up and then populate the actual development DB in my rails app. Thus, I need to sanitize the data as it comes out of the Business model and so perhaps there is no way to shorten the method, but regardless, the method is not working. Commented Mar 12, 2012 at 2:03
  • Please do not define an initialize method inside a model class within Rails. I am assuming of course that this class inherits from ActiveRecord::Base. The reason why I suggest not doing this is because it would override the initialize method that comes from ActiveRecord::Base. Commented Mar 12, 2012 at 2:35

1 Answer 1

4

You could try (pseudocode/untested):

def initialize(business)
    puts 'inside Listing.initialize'

    @attributes.merge(business.attributes)

    puts 'Created a new Listing'

end
Sign up to request clarification or add additional context in comments.

2 Comments

@banditKing i've used this only if both models are ActiveRecord models and only outside of the initializer method. If nobody knows a better answer, try to play with it a little. I would also delete your edits, I think they are confusing b/c you have now basically to questions in one.
oh @wintersolutions you saved my life, I was searching for this for 2 days and almost lost a hope!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.