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
Businessto theListing?Businessinstance as an attribute of theListingobject.Listingcould have additional attributes but the originalBusinessattributes would only reside in one place (DRY).initializemethod 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 theinitializemethod that comes fromActiveRecord::Base.